Pre-work

Install R and RStudio

  1. 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.

  2. 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.

If you have a Mac, make sure you choose correctly between the Apple Silicon (M1/M2) and Intel options.Which version of RStudio you have is not as important, but it’s nice to stay up-to-date for the newest features!

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

  1. Sections 1.4-1.5 of R for Data Science. Run the code in your RStudio console as you go.

  2. Chapter 3 of R for Data Science. Again, run the code in your RStudio console as you read. Try the exercises.

  3. 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.

In particular, make sure you install the packages in the text.If you’re wondering what happened to chapter 2, we’ll be doing that one together!

Challenges

Try these challenges to familiarize yourself with R and RStudio. I’ll provide answers at the beginning of class!

  1. 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.
  2. Create a second vector with the city names called city.
  3. 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 original temp vector with the named vector. Print the temp vector.
  4. 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 return 40). Use this [] operator and the sum() function to calculate the sum of the temperatures for Sydney, Mexico City and Johannesburg.
  5. Create a vector of all positive even numbers smaller than 75 using the seq() function. (Hint: help(seq))
  6. 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?
  7. Using your answer to the previous question, find another solution to create the same vector as the previous question.

Resources