# 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 Materials from lab

# 2 Google Colab

True or false? We can 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?

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

# 3 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))`

# 4 Vectors

Which of the following returns the vector

`20 22 24 26`

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?::: {.cell} ::: {.cell-output .cell-output-stdout}

`num [1:4] 1 0 1 0`

::: :::

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`

# 5 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`

Suppose we run the following code. What will

`df$y[4]`

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

# 6 Missing Values

Suppose we run the following code. What will

`is.na(y)`

return?`<- c(25, 25, NA, 36) y`

Suppose we run the following code. What will

`is.null(y)`

return?`<- c() y`

Suppose we run the following code. What will

`mean(y)`

return?`<- c() y`