R Questions

Oct 31, 2013 • Denis Haine

Questions regarding R:

Q1 (novice to competent): given the following vector x <- c(1 , 2, 5, 8, NA, 10), how do you compute its mean?
a) mean(x)
b) mean(x, na.rm = FALSE)
c) mean(x, na.rm = TRUE)
d) sum(x, na.rm = TRUE) / length(x)

Q2 (competent to expert): given 2 vectors of equal length x and y, why is
z <- x + y
faster to run than
for (i in 1:length(x)) z[i] <- x[i] + y[i] ?
a) there are both executed at the same speed as they involves the same operations
b) the second option is faster as loops are always faster in R
c) for, : and each operation on a vector subscript with [ are function calls which represents computing overhead, and thus the first proposition is faster