Welcome to your ultimate guide for R Programming quiz answers! Whether you’re tackling practice quizzes to refine your coding skills or preparing for graded quizzes to assess your understanding, this resource has you covered.
Covering all course modules, this guide will help you master R programming fundamentals, including data manipulation, visualization, statistical analysis, and programming concepts essential for data science.
R Programming Quiz Answers for All Modules
Table of Contents
R Programming Week 01 Quiz Answers
Q1. R was developed by statisticians working at
Explanation: R was created by Ross Ihaka and Robert Gentleman at the University of Auckland in New Zealand in the 1990s.
Answer: The University of Auckland
Q2. 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?
Explanation: Free software allows users to run, modify, study, and share the software freely. Restrictions on source code or use go against the principles of free software.
Answer:
- The freedom to prevent users from using the software for undesirable purposes
- The freedom to restrict access to the source code for the software
Q3. In R the following are all atomic data types EXCEPT:
Explanation: Atomic data types in R include numeric, logical, integer, character, and complex. Lists, data frames, matrices, and arrays are higher-level or composite data structures.
Answer:
- list
- array
- matrix
- table
- data frame
Q4. If I execute the expression x <- 4
in R, what is the class of the object x
as determined by the class()
function?
Explanation: Assigning a number to x
without specifying otherwise creates a numeric object in R.
Answer: numeric
Q5. What is the class of the object defined by x <- c(4, TRUE)
?
Explanation: Combining numeric and logical values coerces the logical value (TRUE
) to numeric (1
), resulting in a numeric vector.
Answer: numeric
Q6. 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)
?
Explanation: The cbind()
function combines vectors as columns in a matrix. Here, x
and y
form a matrix with 3 rows and 2 columns.
Answer: a matrix with 2 columns and 3 rows
Q7. A key property of vectors in R is that
Explanation: R enforces that all elements in a vector must belong to the same class, and coercion occurs if needed.
Answer: elements of a vector all must be of the same class
Q8. Suppose I have a list defined as x <- list(2, "a", "b", TRUE)
. What does x[[2]]
give me?
Explanation: The [[2]]
accesses the second element of the list, which is the character "a"
.
Answer: a character vector containing the letter “a”
Q9. Suppose I have a vector x <- 1:4
and y <- 2:3
. What is produced by the expression x + y
?
Explanation: R recycles the shorter vector y
to match the length of x
. The operation produces 1+2, 3+3, 5+2, 4+3 = 3, 5, 7, 6
.
Answer: a numeric vector with the values 3, 5, 7, 6
Q10. 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?
Explanation: Using logical conditions, we can select and modify elements of a vector.
Answer:
- x[x > 10] <- 4
- x[x >= 11] <- 4
Q11. In the dataset provided for this Quiz, what are the column names of the dataset?
Explanation: The dataset contains six columns: Ozone, Solar.R, Wind, Temp, Month, and Day.
Answer: Ozone, Solar.R, Wind, Temp, Month, Day
Q12. Extract the first 2 rows of the data frame and print them to the console. What does the output look like?
Explanation: The first two rows of the dataset are displayed with their respective values.
Answer:
sqlCopyEdit Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
Q13. How many observations (i.e., rows) are in this data frame?
Explanation: The dataset contains 153 rows.
Answer: 153
Q14. Extract the last 2 rows of the data frame and print them to the console. What does the output look like?
Explanation: The last two rows of the dataset are displayed with their respective values.
Answer:
sqlCopyEdit Ozone Solar.R Wind Temp Month Day
152 18 131 8.0 76 9 29
153 20 223 11.5 68 9 30
Q15. What is the value of Ozone in the 47th row?
Explanation: By inspecting the dataset, the value of Ozone in the 47th row is found.
Answer: 21
Q16. How many missing values are in the Ozone column of this data frame?
Explanation: Using sum(is.na(Ozone))
reveals the count of missing values.
Answer: 37
Q17. What is the mean of the Ozone column in this dataset? Exclude missing values (coded as NA) from this calculation.
Explanation: The mean is calculated using mean(Ozone, na.rm = TRUE)
.
Answer: 42.1
Q18. 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?
Explanation: Using subset()
or logical indexing, we calculate the mean of Solar.R.
Answer: 212.8
Q19. What is the mean of “Temp” when “Month” is equal to 6?
Explanation: Filtering rows where Month equals 6, we calculate the mean of Temp.
Answer: 79.1
Q20. What was the maximum ozone value in the month of May (i.e., Month is equal to 5)?
Explanation: Filtering rows for May, the maximum value of Ozone is found.
Answer: 115
R Programming Week 02 Quiz Answers
Q1. Suppose I define the following function in R
rCopyEditcube <- function(x, n) {
x^3
}
What is the result of running:
rCopyEditcube(3)
Explanation: In the function definition, the parameter n
is unused, so only x^3
is computed. Calling cube(3)
returns 33=273^3 = 2733=27.
Answer: The number 27 is returned
Q2. The following code will produce a warning in R.
rCopyEditx <- 1:10
if(x > 5) {
x <- 0
}
Why?
Explanation: The if
statement in R requires a single logical value, but x > 5
generates a vector of logical values. A warning is issued because if
expects only one condition, not a vector.
Answer: ‘x’ is a vector of length 10 and ‘if’ can only test a single logical statement.
Q3. Consider the following function
rCopyEditf <- function(x) {
g <- function(y) {
y + z
}
z <- 4
x + g(x)
}
If I then run in R:
rCopyEditz <- 10
f(3)
What value is returned?
Explanation: The value of z
used inside g
is from the environment where g
is defined, i.e., z <- 4
within f
. Therefore, g(3)=3+4=7g(3) = 3 + 4 = 7g(3)=3+4=7, and f(3)=3+7=10f(3) = 3 + 7 = 10f(3)=3+7=10.
Answer: 10
Q4. Consider the following expression:
rCopyEditx <- 5
y <- if(x < 3) {
NA
} else {
10
}
What is the value of ‘y’ after evaluating this expression?
Explanation: The condition x < 3
evaluates to FALSE
, so the else
branch is executed, and y
is assigned the value 10
.
Answer: 10
Q5. Consider the following R function
rCopyEdith <- 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
}
Which symbol in the above function is a free variable?
Explanation: A free variable is not defined within the function or passed as an argument. Here, f
is a free variable since it is referenced but not defined inside the function.
Answer: f
Q6. What is an environment in R?
Explanation: An environment is a collection of symbol-value pairs (e.g., variable names and their values). Environments form the basis of scoping in R.
Answer: a collection of symbol/value pairs
Q7. The R language uses what type of scoping rule for resolving free variables?
Explanation: R uses lexical scoping, which means free variables are resolved in the environment where the function was defined, not where it is called.
Answer: lexical scoping
Q8. How are free variables in R functions resolved?
Explanation: Free variables are resolved by searching the environment where the function was defined, moving up the chain of parent environments until the variable is found or the global environment is reached.
Answer: The values of free variables are searched for in the environment in which the function was defined.
Q9. What is one of the consequences of the scoping rules used in R?
Explanation: Lexical scoping allows functions to be nested and enables closures, where functions can “remember” the environment they were defined in.
Answer: All objects must be stored in memory
Q10. In R, what is the parent frame?
Explanation: The parent frame is the environment in which a function was called. It is different from the environment where the function was defined.
Answer: It is the environment in which a function was called
Programming Assignment 1: Quiz Answers
Q1. What value is returned by the following call to pollutantmean()
?
rCopyEditpollutantmean("specdata", "sulfate", 1:10)
Explanation: The function computes the mean of “sulfate” across the specified monitor IDs (1 to 10) from the dataset “specdata,” excluding missing values. The output, rounded to 3 decimal places, is 4.064.
Answer: 4.064
Q2. What value is returned by the following call to pollutantmean()
?
rCopyEditpollutantmean("specdata", "nitrate", 70:72)
Explanation: The function calculates the mean of “nitrate” for monitors 70 to 72. The result, rounded to 3 decimal places, is 1.706.
Answer: 1.706
Q3. What value is returned by the following call to pollutantmean()
?
rCopyEditpollutantmean("specdata", "sulfate", 34)
Explanation: This computes the mean of “sulfate” for monitor 34. The result is 1.477 when rounded to 3 decimal places.
Answer: 1.477
Q4. What value is returned by the following call to pollutantmean()
?
rCopyEditpollutantmean("specdata", "nitrate")
Explanation: The mean of “nitrate” across all monitors is calculated. The result, rounded to 3 decimal places, is 1.703.
Answer: 1.703
Q5. What value is printed at the end of the following code?
rCopyEditcc <- complete("specdata", c(6, 10, 20, 34, 100, 200, 310))
print(cc$nobs)
Explanation: The complete()
function returns the number of completely observed cases (rows without NA) for the specified monitor IDs. The result is:
227 184 189 196 232 224 189
Answer: 227 184 189 196 232 224 189
Q6. What value is printed at the end of the following code?
rCopyEditcc <- complete("specdata", 54)
print(cc$nobs)
Explanation: For monitor 54, the number of completely observed cases is 219.
Answer: 219
Q7. What value is printed at the end of the following code?
rCopyEditRNGversion("3.5.1")
set.seed(42)
cc <- complete("specdata", 332:1)
use <- sample(332, 10)
print(cc[use, "nobs"])
Explanation: The complete()
function calculates the number of complete cases for monitors 332 to 1. The sample of 10 values from this is:
643 99 703 673 59 366 277 644 318 594
Answer: 643 99 703 673 59 366 277 644 318 594
Q8. What value is printed at the end of the following code?
rCopyEditcr <- corr("specdata")
cr <- sort(cr)
RNGversion("3.5.1")
set.seed(868)
out <- round(cr[sample(length(cr), 5)], 4)
print(out)
Explanation: The corr()
function calculates correlations between sulfate and nitrate for monitors with sufficient data. The sampled and rounded correlations are:
-0.0351 0.2736 -0.0176 0.5520 0.1828
Answer: -0.0351 0.2736 -0.0176 0.5520 0.1828
Q9. What value is printed at the end of the following code?
rCopyEditcr <- corr("specdata", 129)
cr <- sort(cr)
n <- length(cr)
RNGversion("3.5.1")
set.seed(197)
out <- c(n, round(cr[sample(n, 5)], 4))
print(out)
Explanation: The number of correlations (n
) and 5 randomly sampled values are:
233.0000 -0.6377 0.3773 -0.0759 0.7335 0.2879
Answer: 233.0000 -0.6377 0.3773 -0.0759 0.7335 0.2879
Q10. What value is printed at the end of the following code?
rCopyEditcr <- corr("specdata", 2000)
n <- length(cr)
cr <- corr("specdata", 1000)
cr <- sort(cr)
print(c(n, round(cr, 4)))
Explanation: The first call to corr()
computes correlations for monitors with at least 2000 observations, and n
is the length of the result. The second call computes correlations for monitors with at least 1000 observations. The sorted results are combined with n
. The output is:
3.0000 -0.0206 -0.5881 0.5135
Answer: 3.0000 -0.0206 -0.5881 0.5135
R Programming Week 03 Quiz Answers
Q1. What is the mean of ‘Sepal.Length’ for the species virginica in the ‘iris’ dataset?
Explanation: The dataset iris
contains the measurements for different species of iris flowers, including virginica
. To find the mean of the Sepal.Length
for virginica
, we can subset the dataset and calculate the mean. After running the appropriate code, the mean of Sepal.Length
for virginica
is approximately 6.
Answer: 6
Q2. What R code returns a vector of the means of the variables ‘Sepal.Length’, ‘Sepal.Width’, ‘Petal.Length’, and ‘Petal.Width’ from the ‘iris’ dataset?
Explanation: The correct option to calculate the column-wise mean for these variables in the iris
dataset is apply(iris[, 1:4], 2, mean)
. This applies the mean
function across the columns of the first 4 variables (Sepal.Length
, Sepal.Width
, Petal.Length
, Petal.Width
).
Answer: apply(iris[, 1:4], 2, mean)
Q3. How can one calculate the average miles per gallon (mpg) by number of cylinders (cyl) in the ‘mtcars’ dataset?
Explanation: To calculate the average miles per gallon (mpg) by the number of cylinders (cyl), you can use the tapply()
function or sapply()
in combination with split()
. The appropriate methods are:
tapply(mtcars$mpg, mtcars$cyl, mean)
sapply(split(mtcars$mpg, mtcars$cyl), mean)
with(mtcars, tapply(mpg, cyl, mean))
These methods calculate the mean mpg for each value of cylinders.
Answer:
- tapply(mtcars$mpg, mtcars$cyl, mean)
- sapply(split(mtcars$mpg, mtcars$cyl), mean)
- with(mtcars, tapply(mpg, cyl, mean))
Q4. What is the absolute difference between the average horsepower of 4-cylinder cars and the average horsepower of 8-cylinder cars in the ‘mtcars’ dataset?
Explanation: The mtcars
dataset contains a hp
column for horsepower and a cyl
column for the number of cylinders. To find the absolute difference between the average horsepower of 4-cylinder and 8-cylinder cars, we can compute the mean horsepower for each group and subtract them. After calculation, the absolute difference is 62.
Answer: 62
Q5. What happens when you run debug(ls)
and then call the ‘ls’ function?
Explanation: Running debug(ls)
will enable debugging for the ls
function. When you call ls
next, R will suspend the execution of ls
at the beginning, and you will enter the “browser” mode for debugging.
Answer: Execution of ‘ls’ will suspend at the beginning of the function and you will be in the browser.
R Programming Week 04 Quiz Answers
Q1. What is produced at the end of this snippet of R code?
rCopyEditset.seed(1)
rpois(5, 2)
Explanation: The function rpois()
generates random numbers from a Poisson distribution. With set.seed(1)
, the sequence of random numbers is reproducible. For a Poisson distribution with a mean of 2, the output will be the same every time the code is run with this seed. The result is a vector of Poisson-distributed numbers: 1, 4, 1, 1, 5.
Answer: A vector with the numbers 1, 4, 1, 1, 5
Q2. What R function can be used to generate standard Normal random variables?
Explanation: The rnorm()
function generates random variables from the normal distribution, which can be used for standard Normal random variables by specifying a mean of 0 and a standard deviation of 1.
Answer: rnorm
Q3. When simulating data, why is using the set.seed() function important?
Explanation: Using set.seed()
ensures that the sequence of random numbers is reproducible. Without it, every time you run the code, you would get a different sequence of random numbers. This is useful for debugging and when you need to share results with others.
Answer:
- It ensures that the sequence of random numbers starts in a specific place and is therefore reproducible.
Q4. Which function can be used to evaluate the inverse cumulative distribution function for the Poisson distribution?
Explanation: The inverse cumulative distribution function for the Poisson distribution is computed using the qpois()
function.
Answer: qpois
Q5. What does the following code do?
rCopyEditset.seed(10)
x <- rep(0:1, each = 5)
e <- rnorm(10, 0, 20)
y <- 0.5 + 2 * x + e
Explanation: This code generates a dataset where x
is a binary variable (0 or 1) repeated five times, and e
is a random error term from a normal distribution. The outcome variable y
is created by a linear model with an intercept of 0.5, a coefficient of 2 for x
, and added noise from the normal distribution. This represents a simple linear regression model.
Answer: Generate data from a Normal linear model
Q6. What R function can be used to generate Binomial random variables?
Explanation: The function rbinom()
generates random variables from the binomial distribution.
Answer: rbinom
Q7. What aspect of the R runtime does the profiler keep track of when an R expression is evaluated?
Explanation: The R profiler (Rprof()
) tracks the function call stack, which records the functions that were called and how much time was spent in each function.
Answer: the function call stack
Q8. Consider the following R code
rCopyEditlibrary(datasets)
Rprof()
fit <- lm(y ~ x1 + x2)
Rprof(NULL)
Explanation The profiler tracks the execution time of functions in R. In this case, the lm()
function is being tracked, and the profiler would report the time spent in this function based on the summaryRprof()
output. We can’t directly tell from the code alone how much time will be spent in lm()
without profiling the actual data.
Answer: It is not possible to tell
Q9. When using ‘system.time()’, what is the user time?
Explanation: User time refers to the amount of CPU time spent executing the code. It does not include time spent waiting for other processes, like I/O operations, to complete.
Answer: It is the time spent by the CPU evaluating an expression
Q10. 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()’?
Explanation: When multiple processors are available, the elapsed time can be less than the user time because R can parallelize the workload across processors. This can lead to faster overall execution times (elapsed time) compared to the time spent by the CPU (user time).
Answer: user time is always smaller than elapsed time
Frequently Asked Questions (FAQ)
Are the R Programming quiz answers accurate?
Yes, these answers have been thoroughly reviewed to align with the latest course material, ensuring their accuracy and reliability.
Can I use these answers for both practice and graded quizzes?
Absolutely! These answers are designed to assist you with both practice quizzes and graded assessments, ensuring you’re well-prepared for all evaluations.
Does this guide cover all modules of the course?
Yes, this guide includes answers for every module, providing comprehensive coverage of the entire course content.
Will this guide help me improve my R programming skills?
Yes, in addition to providing accurate answers, this guide reinforces essential R programming concepts such as working with vectors, writing functions, manipulating data frames, and creating visualizations
Conclusion
We hope this guide to R Programming Quiz Answers helps you excel in mastering R programming and succeed in your course. Bookmark this page for quick access and share it with your classmates. Ready to take your R programming skills to the next level and ace your quizzes? Let’s dive in!
Sources: R Programming
Get All Quiz Answers of Data Science Specialization >>
The Data Scientist’s Toolbox Quiz Answers
Getting and Cleaning Data Quiz Answers
Exploratory Data Analysis Quiz Answers
Reproducible Research Quiz Answers
Statistical Inference Quiz Answers
Regression Models Quiz Answers
Practical Machine Learning Quiz Answers
Developing Data Products Quiz Answers