Problem Set 1: Code Basics

Due: 5pm on Friday, January 9.

The goal of this problem set is to learn the basics of coding in R.

Create an R script named pset1.R. Write code following the steps below. Submit pset1.R by uploading it on Gradescope (accessible via BruinLearn).

We will grade by running the code and evaluating whether it produces the requested objects. You do not have to label the question numbers in your code.

Much of the material for this problem set corresponds to R4DS Chapter 2. If you are stuck, look at examples there!

  1. Working with numbers.
    1. Store the value 1 in a new object called number_one.
    2. Create a vector of length two holding two values: 1 and 2. You can use the c() function described in the chapter. Store your vector in an object called one_and_two.
    3. Create an object called doubled. Multiply one_and_two by 2 and store the result in this object.
    4. Create an object called summed. Use the sum() function to find the sum of one_and_two. Store the result in summed.
  2. Working with data.
    1. Load data from the course website with the code read_csv(file = "https://soc114.github.io/data/lifeCourse.csv"). Store it in an object called lifeCourse.
      • For help, see R4DS Ch 7.2.
      • This code requires that the tidyverse package is installed and loaded. See R4DS 1.1.1.
    2. Use the nrow() function to find the number of rows in the data. Store the result in an object called num_rows.
    3. Use the ncol() function to find the number of rows in the data. Store the result in an object called num_cols.
Back to top