R Programming Questions and Answers – Loop Functions – 1

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

Answer: a
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

Answer: c
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

Answer: c
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

Answer: d
Explanation: The mapply() function is a multivariate apply of sorts which applies a function in parallel over a set of arguments.

advertisement
advertisement

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

Answer: d
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

Answer: a
Explanation: The apply() function is used to a evaluate a function (often an anonymous one) over the margins of an array.

Note: Join free Sanfoundry classes at Telegram or Youtube

7. _______ is used to apply a function over subsets of a vector.
a) apply()
b) lapply()
c) tapply()
d) mapply()
View Answer

Answer: c
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

Answer: c
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.

advertisement

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)

advertisement
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))
}
View Answer
Answer: a
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
View Answer
Answer: a
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.

To practice all areas of R Programming Language, Here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.