Lab 1: Getting started with R

Not graded, just practice

Published

August 29, 2024

To learn to program in R (or any language), you can read about how to do it, and watch someone else do it; but the only way to really learn is to do it yourself. Create some data structures, try some stuff, and see what happens! Here are some practice quiz questions to guide your learning. We will go over the solutions to these in lab.

1 Materials from lab

2 Google Colab

  1. True or false? We can start a new R notebook in Google Colab with File > New notebook

  2. For problem sets, how will you submit your colab notebook for grading?

  3. What version of R is Google Colab running? Hint: use sessionInfo().

  4. What is the relationship between R and Google Colab?

  5. What happens to files you upload to google colab when the Runtime environment is restarted?

3 R Basics

  1. Which of the following are expressions?

  2. Which of the following are valid variable names in R?

  3. Suppose we open a new colab notebook and run the following code block. What will be returned?

    x <- 1 + 2
    y <- 0 + 3
    ls()
  4. Which of the following will load the emo package into the current environment?

  5. Which of the following occur in the code block below?

    # compute the mean of x and y
    mean(c(x,y))

4 Vectors

  1. Which of the following returns the vector 20 22 24 26

  2. Suppose we construct a vector with c(1, "two", 3, 4, 5, 6) and assign it to x. What will the following code block return?

    typeof(x)

    What is the previous question an example of?

  3. What will the following code block return?

    x <- 1:4
    y <- matrix(x, ncol=2, nrow=2)
    typeof(y)

  4. What will the following code block return?

    x <- c()
    length(x)

  5. Given the following output from str(x), what will as.logical(x) return?

    ::: {.cell} ::: {.cell-output .cell-output-stdout}

     num [1:4] 1 0 1 0

    ::: :::

  6. Given the following vector, what will as.double(x) return?

    x <- c("one", "two", "three")
  7. What happens if you add a vector of numbers to a single number in the following expression?

    c(1, 3, 5) + 1
  8. What happens if you multiply a vector times another vector?

    c(1, 3, 5) * c(10, 100, 1000)
  9. Suppose we run the following code. What will any(x) return?

    x <- c(1, 5, 11) > 10

5 Subsetting

  1. Which of the following code subsets the vector x <- c("blue", "pink", "red") to return just the first element?

  2. Suppose we run the following code. What will x[[2]] return?

    x <- seq(from = 2, to =8, by=2)

  3. Suppose we run the following code. What will m[1, 2] return?

    m <- matrix(c(10,20,30,40), nrow=2, ncol=2)

  4. Suppose we run the following code. What will df$y[4] return?

    df <- data.frame(
        x = c(2, 4, 6, 8), 
        y = c("l", "m", "n", "o")
    )

6 Missing Values

  1. Suppose we run the following code. What will is.na(y) return?

    
    y <- c(25, 25, NA, 36)
  2. Suppose we run the following code. What will is.null(y) return?

    y <- c()

  3. Suppose we run the following code. What will mean(y) return?

    y <- c()