Lab 1: Getting started with R

Not graded, just practice

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 Google Colab

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

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

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

What is the relationship between R and Google Colab?

2 R Basics

Which of the following are expressions?

Which of the following are valid variable names in R?

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

x <- 1 + 2
y <- 0 + 3
ls()

Which of the following will load the emo package into the current environment?

Which of the following occur in the code block below?

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

3 Vectors

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?

What will the following code block return?

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

What will the following code block return?

x <- c()
length(x)

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

 num [1:4] 1 0 1 0

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

x <- c("one", "two", "three")

What happens if you add a vector of numbers to a single number in the following expression?

c(1, 3, 5) + 1

What happens if you multiply a vector times another vector?

c(1, 3, 5) * c(10, 100, 1000)

Suppose we run the following code. What will any(x) return?

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

4 Subsetting

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

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

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

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

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