This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Loop Functions – 1”.
1. ________ loop over a list and evaluate a function on each element.
a) apply()
b) lapply()
c) sapply()
d) mapply()
View Answer
Explanation: An auxiliary function split is also useful, particularly in conjunction with lapply.
2. Point out the wrong statement?
a) Multi-line expressions with curly braces are just not that easy to sort through when working on the command line
b) lappy() loops over a list, iterating over each element in that list
c) lapply() does not always returns a list
d) You cannot use lapply() to evaluate a function multiple times each with a different argument
View Answer
Explanation: lapply() always returns a list, regardless of the class of the input.
3. __________ function is same as lapply in R.
a) apply()
b) lapply()
c) sapply()
d) mapply()
View Answer
Explanation: lapply tries to simplify the result.
4. Which of the following is multivariate version of lapply?
a) apply()
b) lapply()
c) sapply()
d) mapply()
View Answer
Explanation: The mapply() function is a multivariate apply of sorts which applies a function in parallel over a set of arguments.
5. Point out the correct statement?
a) lapply() takes elements of the list and passes them as the first argument of the function you are applying
b) You can use lapply() to evaluate a function multiple times each with a different argument
c) Functions that you pass to lapply() may have other arguments
d) None of the mentioned
View Answer
Explanation: The lapply() function and its friends make heavy use of anonymous functions.
6. ________ applies a function over the margins of an array.
a) apply()
b) lapply()
c) tapply()
d) mapply()
View Answer
Explanation: The apply() function is used to a evaluate a function (often an anonymous one) over the margins of an array.
7. _______ is used to apply a function over subsets of a vector.
a) apply()
b) lapply()
c) tapply()
d) mapply()
View Answer
Explanation: t in tapply stands for table.
8. lappy functions takes _________ arguments in R language.
a) two
b) three
c) four
d) five
View Answer
Explanation: This function takes three arguments: (1) a list X; (2) a function (or the name of a function) FUN; (3) other arguments via its … argument.
9. Body of lapply function is?
a)
function (X, FUN, ...) { FUN <- match.fun(FUN) if (!is.vector(X) || is.object(X)) X <- as.list(X) .Internal(lapply(X, FUN)) }
b)
function (X, FUN, ...) { FUN <- match.fun(FUN) if (!is.vector(X) | is.object(X)) X <- as.list(X) .Internal(lapply(X, FUN)) }
c)
function (X, FUN, ...) { FUN <- match.fun(FUN) if (is.vector(X) || is.object(X)) X <- as.list(X) .Internal(lapply(X, FUN)) }
d)
function (X, FUN, ...) { FUN <- match.fun(FUN) if (is.vector(X) || is.object(X)) }
Explanation: Actual looping is done internally in C code for efficiency reasons.
10. What will be the output of the following R code?
> x <- list(a = 1:5, b = rnorm(10)) > lapply(x, mean)
a)
$a [1] 3 $b [1] 0.1322028
b)
$a [1] 4 $b [1] 0.1322028
c)
$a [1] 5 $b [1] 0.1322028
d)
$a [2] 5 $b [1] 3
Explanation: If the original list has names, the the names will be preserved in the output.
Sanfoundry Global Education & Learning Series – R Programming Language.
Here’s the list of Best Books in R Programming Language.
- Practice Programming MCQs
- Check R Programming Books
- Apply for Programming Internship
- Check Information Technology Books