github repo for rest of specialization: Data Science Coursera
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?
The number 27 is returned
The following code will produce a warning in R.
x <- 1:10
if(x > 5) {
x <- 0
}
Why?
'x' is a vector of length 10 and 'if' can only test a single logical statement.
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?
10
Consider the following expression:
x <- 5
y <- if(x < 3) {
NA
} else {
10
}
What is the value of 'y' after evaluating this expression?
10
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?
f
What is an environment in R?
a collection of symbol/value pairs
The R language uses what type of scoping rule for resolving free variables?
lexical scoping
How are free variables in R functions resolved?
The values of free variables are searched for in the environment in which the function was defined
What is one of the consequences of the scoping rules used in R?
All objects must be stored in memory Correct 1.00
In R, what is the parent frame?
It is the environment in which a function was called
install.packages("data.table")
library("data.table")
pollutantmean <- function(directory, pollutant, id = 1:332) {
Format number with fixed width and then append .csv to number
fileNames <- paste0(directory, '/', formatC(id, width=3, flag="0"), ".csv" )
Reading in all files and making a large data.table
lst <- lapply(fileNames, data.table::fread)
dt <- rbindlist(lst)
if (c(pollutant) %in% names(dt)){
return(dt[, lapply(.SD, mean, na.rm = TRUE), .SDcols = pollutant][[1]])
}
}
Example usage
pollutantmean(directory = '~/Desktop/specdata', pollutant = 'sulfate', id = 20)