You are on page 1of 63

R

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

R is a dialect of the S language which was developed at Bell Labs.

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

This is not part of the free software definition. Freedom 0 requires


that the users of free software be free to use the software for any
purpose.

The freedom to study how the

program works, and adapt it to your


needs.

The freedom to run the program, for

any purpose.

The freedom to redistribute copies so

you can help your neighbor.

The freedom to prevent users from

Corre
ct

using the software for undesirable


purposes.

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

'list' is not an atomic data type in R.

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

The 'L' suffix creates an integer vector as opposed to a numeric vector.

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

columns and 3 rows

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

elements of a vector can be of different classes

the length of a vector must be less than 32,768

elements of a vector all must be of the same class

elements of a vector can only be character or numeric

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

a list containing the number 2.

a character vector containing the element "2".

a list containing the letter "a".

Score

Explanation

a numeric vector of length 1.

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

a numeric vector with elements 3, 2, 3, 6.

a numeric vector with elements 3, 4, 5, 6.

Correct

1.00

Explanation

an integer vector with elements 3, 2, 3, 6.

an integer vector with elements 3, 2, 3, 4.

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

x[x > 10]

<- 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.

x[x > 10]

== 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.

Ozone, Solar.R, Wind

1, 2, 3, 4, 5, 6

Month, Day, Temp, Wind

Ozone, Solar.R, Wind, Temp, Month,

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.

Ozone Solar.R Wind Temp


Month Day
1

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

Ozone Solar.R Wind Temp


Month Day

24 10.9 71

18

131 8.0 76

9 14
9 29

Ozone Solar.R Wind Temp


Month Day
1

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

Ozone Solar.R Wind Temp Month


Day
152

11

153 108

44 9.7 62
223 8.0 85

5 20
7 25

Ozone Solar.R Wind Temp Month


Day
152

34

307 12.0 66

153

13

27 10.3 76

5 17
9 18

Ozone Solar.R Wind Temp Month


Day
152

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
}

What is the result of running


cube(3)

in R after defining this function?

Your Answer

The users is prompted to specify the value of

'n'.

A warning is given with no value returned.

Score

Explanation

The number 27 is returned

Corre
ct

1.00

An error is returned because 'n' is not specified

in the call to 'cube'

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

Because 'n' is not evaluated, it is not needed even though


it is a formal argument.

Your Answer

Score

The expression uses curly braces.

The syntax of this R expression is incorrect.

There are no elements in 'x' that are greater than 5

You cannot set 'x' to be 0 because 'x' is a vector and 0 is a scalar.

'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)

What value is returned?

Your Answer

Score

16

10

Total

Question 4

Correct

1.00

1.00 / 1.00

Explanation

Consider the following expression:


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

What is the value of 'y' after evaluating this expression?

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
}

Which symbol in the above function is a free variable?

Your Answer

Score

Correct

1.00

Explanation

Total

1.00 / 1.00

Question 6
What is an environment in R?

Your Answer

a collection of symbol/value pairs

Score

Correct

1.00

Explanation

a special type of function

an R package that only contains data

a list whose elements are all functions

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

All objects must be stored in memory

All objects can be stored on the disk

Functions cannot be nested

R objects cannot be larger than 100 MB

Score

Correct

1.00

Explanation

Total

1.00 / 1.00

Question 10
In R, what is the parent frame?

Your Answer

Score

It is always the global environment

It is the environment in which a function was defined

It is the environment in which a function was called

Correct

1.00

Explanation

It is the package search list

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)

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.)

Answer for Question 1


You entered:

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

apply(iris[, 1:4], 2, mean)

Score

Correct

1.00

apply(iris[, 1:4], 1, mean)

apply(iris, 1, mean)

colMeans(iris)

Total

Question 3

1.00 / 1.00

Explanation

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)?

Your Answer

sapply(split(mtcars$mpg, mtcars$cyl), mean)

sapply(mtcars, cyl, mean)

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)

what happens when you next call the 'ls' function?

Your Answer

Execution of the 'ls' function will suspend at the 4th line of the function and you will be in the

browser.

The 'ls' function will execute as usual.

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

and enter the browser.

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

A vector with the numbers 1, 4, 1,

Score

Explanation

1, 5

It is impossible to tell because the

result is random

A vector with the numbers 1, 1, 2,

Corre
ct

1.00

4, 1

A vector with the numbers 3.3, 2.5,

0.5, 1.1, 1.7

Total

Question 2

1.00 /
1.00

Because the `set.seed()' function is used, `rpois()' will always output


the same vector in this code.

What R function can be used to generate standard Normal random variables?

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.

It ensures that the sequence of random numbers is truly random.

Score

Correc
t

1.00

Explanat
on

It can be used to generate non-uniform random numbers.

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

Generate random exponentially distributed data

Generate data from a Normal linear model

Correct

1.00

Generate data from a Poisson generalized linear model

Generate uniformly distributed random data

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

the function call stack

Score

Correct

1.00

the working directory

the global environment

the package search list

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%

It is not possible to tell

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

It is the time spent by the CPU evaluating an expression

It is a measure of network latency

It is the "wall-clock" time it takes to evaluate an expression

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 always smaller than elapsed time

user time is 0

elapsed time may be smaller than user time

Total

Correct

1.00

1.00 / 1.00

You might also like