R Programming course: Quizzes

QUIZ 1

Question 1

R was developed by statisticians working at:

Question 2

The definition of free software consists of four freedoms (freedoms 0 through 3). Which of the following is NOT one of the freedoms that are part of the definition?

Question 3

In R the following are all atomic data types EXCEPT

Question 4

If I execute the expression x <- 4L in R, what is the class of the object x as determined by the class() function?

Question 5

What is the class of the object defined by the expression x <- c(4, ‘a’, TRUE)?

Question 6

If I have two vectors x <- c(1,3, 5) and y <- c(3, 2, 10), what is produced by the expression cbind(x, y)?

Question 7

A key property of vectors in R is that

Question 8

Suppose I have a list defined as x <- list(2, “a”, “b”, TRUE). What does x[[1]] give me?

Question 9

Suppose I have a vector x <- 1:4 and y <- 2:3. What is produced by the expression x + y?

Question 10

Suppose I have a vector x <- c(3, 5, 1, 10, 12, 6) and I want to set all elements of this vector that are less than 6 to be equal to zero. What R code achieves this?

Question 11

In the dataset provided for this Quiz, what are the column names of the dataset?

Question 12

Extract the first 2 rows of the data frame and print them to the console. What does the output look like?

Question 13

How many observations (i.e. rows) are in this data frame?

Question 14

Extract the last 2 rows of the data frame and print them to the console. What does the output look like?

Question 15

What is the value of Ozone in the 47th row?

Question 16

How many missing values are in the Ozone column of this data frame?

Question 17

What is the mean of the Ozone column in this dataset? Exclude missing values (coded as NA) from this calculation.

Question 18

Question 19

What is the mean of “Temp” when “Month” is equal to 6?

Question 20

What was the maximum ozone value in the month of May (i.e. Month = 5)?




QUIZ 2

Question 1

Suppose I define the following function in R

cube <- function(x, n) { x^3 }

What is the result of running cube(3) in R after defining this function?

Question 2

The following code will produce a warning in R.

x <- 1:10 if(x > 5) { x <- 0 }

Why?

Question 3

Consider the following function

f <- function(x) { g <- function(y) { y + z } z <- 4 x + g(x) }

If I then run in R

z <- 10

f(3)

What value is returned?

Question 4

Consider the following expression:

x <- 5

y <- if(x < 3) { NA } else { 10 }

What is the value of “y” after evaluating this expression?

Question 5

Consider the following R function

h <- function(x, y = NULL, d = 3L) {

}

Which symbol in the above function is a free variable?

Question 6

What is an environment in R?

Question 7

The R language uses what type of scoping rule for resolving free variables?

Question 8

How are free variables in R functions resolved?

Question 9

What is one of the consequences of the scoping rules used in R?

Question 10

In R, what is the parent frame? http://stackoverflow.com/questions/7439110/what-is-the-difference-between-parent-frame-and-parent-env-in-r-how-do-they




QUIZ 3

Question 1

Take a look at the ‘iris’ dataset that comes with R. The data can be loaded with the code: library(datasets) data(iris) A description of the dataset can be found by running ?iris There will be an object called ‘iris’ in your workspace. In this dataset, what is the mean of ‘Sepal.Length’ for the species virginica? (Please only enter the numeric result and nothing else.)

Question 2

Continuing with the ‘iris’ dataset from the previous Question, what R code returns a vector of the means of the variables ‘Sepal.Length’, ‘Sepal.Width’, ‘Petal.Length’, and ‘Petal.Width’?

Question 3

Load the ‘mtcars’ dataset in R with the following code library(datasets) data(mtcars) There will be an object names ‘mtcars’ in your workspace. You can find some information about the dataset by running ?mtcars How can one calculate the average miles per gallon (mpg) by number of cylinders in the car (cyl)?

Question 4

Continuing with the ‘mtcars’ dataset from the previous Question, what is the absolute difference between the average horsepower of 4-cylinder cars and the average horsepower of 8-cylinder cars?

Question 5

If you run debug(ls) what happens when you next call the ‘ls’ function?




Quiz4

Question 1

What is produced at the end of this snippet of R code? set.seed(1) rpois(5, 2)

Your Answer Score Explanation

Question 2

What R function can be used to generate standard Normal random variables? Your Answer Score Explanation

Question 3

When simulating data, why is using the set.seed() function important? Your Answer Score Explanation

Question 4

Which function can be used to evaluate the inverse cumulative distribution function for the Poisson distribution? Your Answer Score Explanation

Question 5

What does the following code do? set.seed(10) x <- rep(0:1, each = 5) e <- rnorm(10, 0, 20) y <- 0.5 + 2 * x + e Your Answer Score Explanation

Question 6

What R function can be used to generate Binomial random variables?

Question 7

What aspect of the R runtime does the profiler keep track of when an R expression is evaluated? Your Answer Score Explanation

Question 8

Consider the following R code library(datasets) Rprof() fit <- lm(y ~ x1 + x2) Rprof(NULL) (Assume that y, x1, and x2 are present in the workspace.) Without running the code, what percentage of the run time is spent in the ‘lm’ function, based on the ‘by.total’ method of normalization shown in ‘summaryRprof()’? Your Answer Score Explanation

Question 9

When using ‘system.time()’, what is the user time? Your Answer Score Explanation

Question 10

If a computer has more than one available processor and R is able to take advantage of that, then which of the following is true when using ‘system.time()’? Your Answer Score Explanation