num [1:4] 1 0 1 0
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?
<- 1 + 2
x <- 0 + 3
y 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?
<- 1:4
x <- matrix(x, ncol=2, nrow=2)
y typeof(y)
What will the following code block return?
<- c()
x length(x)
Given the following output from str(x)
, what will as.logical(x)
return?
Given the following vector, what will as.double(x)
return?
<- c("one", "two", "three") x
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?
<- c(1, 5, 11) > 10 x
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?
<- seq(from = 2, to =8, by=2) x
Suppose we run the following code. What will m[1, 2]
return?
<- matrix(c(10,20,30,40), nrow=2, ncol=2) m