Pre-work
Install R and RStudio
Download R from CRAN. Choose the link at the top that corresponds to your operating system. Unless you downloaded R within the past month or two, do so again – you want the most up-to-date version (≥ R 4.4) for this class.
Download RStudio (step 2 on that page – you already completed step 1 above!). It should automatically recognize your operating system, but if not, choose the correct link at the bottom.
Video
Open the slides in a new tab here or follow along below.
(P.S. Sorry, I didn’t realize the video of my face would be in the screen instead of off to the side! Also this video is from last year so the website looks a bit different!)
Readings
Sections 1.4-1.5 of R for Data Science. Run the code in your RStudio console as you go.
Chapter 3 of R for Data Science. Again, run the code in your RStudio console as you read. Try the exercises.
Optional, but helpful: Chapter 2 of Hands-On Programming with R. For the purposes of this class, we will necessarily skip some of the R basics to focus on the skills you’ll need most. This is a good resource if you want to learn more about them, so it’s highly recommended, you just don’t need to master it as part of your pre-work.
Challenges
Try these challenges to familiarize yourself with R and RStudio. I’ll provide answers at the beginning of class!
- Create a vector named
temp
with the average low temperatures in December for London, Shanghai, Sydney, Boston, Mexico City and Johannesburg which are 40, 38, 64, 28, 42 and 57° Fahrenheit. - Create a second vector with the city names called
city
. - Use the
setNames()
function to assign the city names to the corresponding temperatures using the two objects create above. (Hint:help(setNames)
). You should overwrite your originaltemp
vector with the named vector. Print thetemp
vector. - Recall that you can use the
[]
operator directly following a vector name to select specific values contained within that vector (e.g.temp[1]
should return40
). Use this[]
operator and thesum()
function to calculate the sum of the temperatures for Sydney, Mexico City and Johannesburg. - Create a vector of all positive even numbers smaller than 75 using the
seq()
function. (Hint:help(seq)
) - Create a vector of numbers. Let the first value of the vector be 4, with a maximum value < 80, adding numbers in increments of 2/3. How many elements are in this vector?
- Using your answer to the previous question, find another solution to create the same vector as the previous question.