R Programming
R Programming
prog
QUIZ 1 r
programming
Question 1
The R language is a dialect of which of the following programming languages?
Your Answer
Correct
Score
Explanation
1.00
Java
Scheme
Total
1.00 / 1.00
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?
Your Answer
Score
Explanation
1.00
any purpose.
Corre
ct
Total
1.00 /
1.00
Question 3
In R the following are all atomic data types EXCEPT
Your Answer
list
numeric
integer
Correct
Score
Explanation
1.00
complex
Total
1.00 / 1.00
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?
Your Answer
complex
matrix
Score
Explanation
logical
integer
Correct
Total
1.00
1.00 / 1.00
Question 5
What is the class of the object defined by the expression x <- c(4, "a", TRUE)?
Your
Answer
logical
Score
Explanation
integer
mixed
charact
Corre
ct
1.00
The character class is the "lowest common denominator" here and so all elements will be
coerced into that class.
er
Total
1.00 /
1.00
Question Explanation
R does automatic coercion of vectors so that all elements of the vector are the same data class.
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)?
Your Answer
a matrix with 2
Corre
Score
Explanation
1.00
The 'cbind' function treats vectors as if they were columns of a matrix. It then takes
those vectors and binds them together column-wise to create a matrix.
ct
a vector of length 2
a 2 by 2 matrix
a 3 by 3 matrix
Total
1.00 /
1.00
Question 7
A key property of vectors in R is that
Your Answer
Score
Correct
1.00
Explanation
Total
1.00 / 1.00
Question 8
Suppose I have a list defined as x <- list(2, "a", "b", TRUE). What does x[[1]] give me?
Your Answer
Score
Explanation
Correct
Total
1.00
1.00 / 1.00
Question 9
Suppose I have a vector x <- 1:4 and a vector y <- 2. What is produced by the expression x + y?
Your Answer
Score
Correct
1.00
Explanation
Total
1.00 / 1.00
Question 10
Suppose I have a vector x <- c(17, 14, 4, 5, 13, 12, 10) and I want to set all elements of this vector that are greater than 10 to be equal to 4.
What R code achieves this?
Your Answer
<- 4
Corre
ct
Score
Explanation
1.00
You can create a logical vector with the expression x > 10 and then use the [ operator to
subset the original vector x.
== 4
x[x == 4]
> 10
x[x == 10]
<- 4
Total
1.00 /
1.00
Question 11
In the dataset provided for this Quiz, what are the column names of the dataset?
Your Answer
Score
Explanation
1.00
You can get the column names of a data frame with the `names()'
function.
1, 2, 3, 4, 5, 6
Correc
t
Day
Total
1.00 /
1.00
Question 12
Extract the first 2 rows of the data frame and print them to the console. What does the output look like?
Your Answer
Score
Explanation
1.00
You can extract the first two rows using the [ operator and an integer sequence
to index the rows.
35
NA 6.9 74
274 10.3 82
5 11
7 17
Corre
ct
Ozone Solar.R Wind Temp
Month Day
1
41
190 7.4 67
5 1
36
118 8.0 72
5 2
24 10.9 71
18
131 8.0 76
9 14
9 29
18
224 13.8 67
9 17
NA
258 9.7 81
7 22
Total
1.00 /
1.00
Question 13
How many observations (i.e. rows) are in this data frame?
Your Answer
45
Score
Explanation
160
153
Correct
1.00
You can use the `nrows()' function to compute the number of rows in a data frame.
129
Total
1.00 / 1.00
Question 14
Extract the last 2 rows of the data frame and print them to the console. What does the output look like?
Your Answer
Score
Explanation
11
153 108
44 9.7 62
223 8.0 85
5 20
7 25
34
307 12.0 66
153
13
27 10.3 76
5 17
9 18
31
244 10.9 78
153
29
127 9.7 82
8 19
6 7
Correc
t
Ozone Solar.R Wind Temp Month
Day
152
18
131 8.0 76
9 29
1.00
The `tail()' function is an easy way to extract the last few elements of an
R object.
153
20
223 11.5 68
9 30
Total
1.00 /
1.00
Question 15
What is the value of Ozone in the 47th row?
Your Answer
Score
Explanation
1.00
The single bracket [ operator can be used to extract individual rows of a data frame.
34
21
63
Correct
18
Total
1.00 / 1.00
Question 16
How many missing values are in the Ozone column of this data frame?
Your Answer
43
Score
Explanation
78
37
Correct
Total
1.00
1.00 / 1.00
Question Explanation
The `is.na' function can be used to test for missing values.
Question 17
What is the mean of the Ozone column in this dataset? Exclude missing values (coded as NA) from this calculation.
Your Answer
31.5
Score
Explanation
53.2
18.0
42.1
Correct
Total
1.00
1.00 / 1.00
Question Explanation
The `mean' function can be used to calculate the mean.
Question 18
Extract the subset of rows of the data frame where Ozone values are above 31 and Temp values are above 90. What is the mean of Solar.R
in this subset?
Your Answer
Score
Explanation
205.0
334.0
212.8
Correct
1.00
185.9
Total
1.00 / 1.00
Question Explanation
You need to construct a logical vector in R to match the question's requirements. Then use that logical vector to subset the data frame.
Question 19
What is the mean of "Temp" when "Month" is equal to 6?
Your Answer
Score
75.3
85.6
90.2
79.1
Correct
1.00
Explanation
Total
1.00 / 1.00
Question 20
What was the maximum ozone value in the month of May (i.e. Month = 5)?
Your Answer
Score
97
18
115
Correct
1.00
Explanation
100
Total
1.00 / 1.00
QUIZ 2 r
programming
Question 1
Suppose I define the following function in R
cube <- function(x, n) {
x^3
}
Your Answer
'n'.
Score
Explanation
Corre
ct
1.00
Total
Question 2
The following code will produce a warning in R.
x <- 1:10
if(x > 5) {
x <- 0
}
Why?
1.00 /
1.00
Your Answer
Score
'x' is a vector of length 10 and 'if' can only test a single logical statement.
Correct
1.00
Explanation
Total
1.00 / 1.00
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)
Your Answer
Score
16
10
Total
Question 4
Correct
1.00
1.00 / 1.00
Explanation
Your Answer
NA
Score
Explanation
10
Correct
1.00
Total
1.00 / 1.00
Question 5
Consider the following R function
h <- function(x, y = NULL, d = 3L) {
z <- cbind(x, d)
if(!is.null(y))
z <- z + y
else
z <- z + f
g <- x + y / z
if(d == 3L)
return(g)
g <- g + 10
g
}
Your Answer
Score
Correct
1.00
Explanation
Total
1.00 / 1.00
Question 6
What is an environment in R?
Your Answer
Score
Correct
1.00
Explanation
Total
1.00 / 1.00
Question 7
The R language uses what type of scoping rule for resolving free variables?
Your Answer
Score
Explanation
lexical scoping
Correct
1.00
dynamic scoping
compilation scoping
global scoping
Total
Question 8
How are free variables in R functions resolved?
1.00 / 1.00
Your Answer
Score
The values of free variables are searched for in the environment in which the function was
called
The values of free variables are searched for in the global environment
The values of free variables are searched for in the environment in which the function was
Correc
1.00
defined
The values of free variables are searched for in the working directory
Total
1.00 /
1.00
Explanati
on
Question 9
What is one of the consequences of the scoping rules used in R?
Your Answer
Score
Correct
1.00
Explanation
Total
1.00 / 1.00
Question 10
In R, what is the parent frame?
Your Answer
Score
Correct
1.00
Explanation
Total
1.00 / 1.00
QUIZ 3 r
programming
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)
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.)
Your
Answer
6.588
Correc
t
Total
Score
Explanation
1.00
To get the answer here, you can use 'tapply' to calculate the mean of 'Sepal.Length' within
each species.
1.00 /
1.00
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'?
Your Answer
Score
Correct
1.00
apply(iris, 1, mean)
colMeans(iris)
Total
Question 3
1.00 / 1.00
Explanation
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)?
Your Answer
split(mtcars, mtcars$cyl)
Score
Correct
1.00
Explanation
apply(mtcars, 2, mean)
Total
1.00 / 1.00
Question 4
Continuing with the 'mtcars' dataset from the previous Question, what is the absolute difference between the average horsepower of 4cylinder cars and the average horsepower of 8-cylinder cars?
Answer for Question 4
You entered:
Your Answer
Score
Explanation
126.57793
Correct
Total
1.00
1.00 / 1.00
Question 5
If you run
debug(ls)
Your Answer
Execution of the 'ls' function will suspend at the 4th line of the function and you will be in the
browser.
Score
Explana
ion
Execution of 'ls' will suspend at the beginning of the function and you will be in the browser.
Corre
1.00
ct
You will be prompted to specify at which line of the function you would like to suspend execution
Total
1.00 /
1.00
QUIZ 4 r
programming
Question 1
What is produced at the end of this snippet of R code?
set.seed(1)
rpois(5, 2)
Your Answer
Score
Explanation
1, 5
result is random
Corre
ct
1.00
4, 1
Total
Question 2
1.00 /
1.00
Your Answer
Score
Explanation
1.00
Functions beginning with the `r' prefix are used to simulate random variates.
qnorm
rnorm
Correct
dnorm
pnorm
Total
Question Explanation
1.00 / 1.00
Standard probability distributions in R have a set of four functions that can be used to simulate variates, evaluate the density, evaluate the
cumulative density, and evaluate the quantile function.
Question 3
When simulating data, why is using the set.seed() function important?
Your Answer
It ensures that the sequence of random numbers starts in a specific place and is therefore
reproducible.
It ensures that the random numbers generated are within specified boundaries.
Score
Correc
t
1.00
Explanat
on
Total
1.00 /
1.00
Question 4
Which function can be used to evaluate the inverse cumulative distribution function for the Poisson distribution?
Your
Answer
rpois
ppois
Score
Explanation
qpois
Corre
ct
1.00
Probability distribution functions beginning with the `q' prefix are used to evaluate the quantile
(inverse cumulative distribution) function.
dpois
Total
1.00 /
1.00
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
Correct
1.00
Total
Question 6
What R function can be used to generate Binomial random variables?
1.00 / 1.00
Your Answer
Score
pbinom
qbinom
rbinom
Correct
1.00
dbinom
Total
Question 7
1.00 / 1.00
Explanation
What aspect of the R runtime does the profiler keep track of when an R expression is evaluated?
Your Answer
Score
Correct
1.00
Total
1.00 / 1.00
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
100%
Score
Explanation
23%
Inorrect
0.00
50%
Total
0.00 / 1.00
Question 9
When using 'system.time()', what is the user time?
Your Answer
It is the time spent by the CPU waiting for other tasks to finish
Score
Explanation
Total
0.00 / 1.00
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
elapsed time is 0
user time is 0
Total
Correct
1.00
1.00 / 1.00