R Programming Questions and Answers – Loop Functions – 2

This set of R Programming online quiz focuses on “Loop Functions – 2”.

1. What will be the output of the following R code?

> x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
> lapply(x, mean)

a)

$a
[1] 2.5
$b
[1] 1.248845
$c
[1] 1.9935285
Loop Functions 90
$d
[1] 5.051388

b)

advertisement
advertisement
$a
[1] 2.5
$b
[1] 0.248845
$c
[1] 0.9935285
Loop Functions 90
$d
[1] 5.051388

c)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$a
[1] 3.5
$b
[1] 0.248845
$c
[1] 0.9935285
Loop Functions 90
$d
[1] 5.051388

d) Error
View Answer

Answer: b
Explanation: You can use lapply() to evaluate a function multiple times each with a different argument.

advertisement

2. Point out the wrong statement?
a) The sapply() function behaves similarly to lapply()
b) With multiple factors and many levels, creating an interaction can result in many levels that are empty
c) apply() can be thought of as a combination of split() and sapply() for vectors only
d) You can use tsplit() to evaluate a function single time each with a same argument
View Answer

Answer: c
Explanation: tapply() can be thought of as a combination of split() and sapply() for vectors only.

advertisement

3. What will be the output of the following R code?

> x <- 1:4
> lapply(x, runif)

a)

[[1]]
[1] 0.02778712
[[2]]
[1] 0.5273108 0.8803191
[[3]]
[1] 0.37306337 0.04795913 0.13862825
[[4]]
[1] 0.3214921 0.1548316 0.1322282 0.2213059

b)

[[1]]
[1] 1.02778712
[[2]]
[1] 2.5273108 0.8803191
[[3]]
[1] 3.37306337 0.04795913 0.13862825
[[4]]
[1] 0.3214921 0.1548316 0.1322282 0.2213059

c)

[[1]]
[1] 1.02778712
[[2]]
[1] 0.5273108 0.8803191
[[3]]
[1] 0.37306337 0.04795913 0.13862825
[[4]]
[1] 3.3214921 2.1548316 1.1322282 0.2213059

d) Error
View Answer

Answer: a
Explanation: In the above question, the first argument of runif() is n, and so the elements of the sequence 1:4 all got passed to the n argument of runif().

4. Which of the following R code will print the following result?

[[1]]
[1] 2.263808
[[2]]
[1] 1.314165 9.815635
[[3]]
[1] 3.270137 5.069395 6.814425
[[4]]
[1] 0.9916910 1.1890256 0.5043966 9.2925392

a)

> x <- 1:4
> lapply(x, runif, min = 0, max = 10)

b)

> x <- 1:4
> lapply(x, runif, min = 0, max = 9)

c)

> x <- 1:3
> lapply(x, runif, min = 0, max = 10)

d)

> x <- 1:4
> lapply(x, runif, min = 0, max = 6)
View Answer
Answer: a
Explanation: Once the call to lapply() is finished, the function disappears and does not appear in the workspace.
 
 

5. Point out the correct statement?
a) split() takes elements of the list and passes them as the first argument of the function you are applying
b) You can use tsplit() to evaluate a function single time each with a same argument
c) Sequence of operations is sometimes referred to as “map-reduce”
d) apply() can be thought of as a combination of split() and sapply() for vectors only
View Answer

Answer: c
Explanation: The results of applying the function over the subsets are then collated and returned as an object.

6. What will be the output of the following R code?

> x <- list(a = matrix(1:4, 2, 2), b = matrix(1:6, 3, 2))
> lapply(x, function(elt) { elt[,1] })

a)

$a
[1] 1 2
$b
[1] 1 2 3

b)

$a
[1] 1 2 3
$b
[1] 1 2 3

c)

$a
[1] 1 2 3
$b
[1] 1 2

d) Error
View Answer

Answer: a
Explanation: You can put an arbitrarily complicated function definition inside lapply().

7. The _____ function takes a vector or other objects and splits it into groups determined by a factor or list of factors.
a) apply()
b) lsplit()
c) split()
d) mapply()
View Answer

Answer: c
Explanation: The combination of split() and a function like lapply() or sapply() is a common paradigm in R.

8. Which of the following is valid body of split function?
a) function (x, f)
b) function (x, drop = FALSE, …)
c) function (x, f, drop = FALSE, …)
d) function (drop = FALSE, …)
View Answer

Answer: c
Explanation: x is a vector (or list) or data frame.

9. What will be the output of the following R code?

> f <- function(elt) {
+ elt[, 1]
+ }
> lapply(x, f)

a)

$a
[1] 1 2
$b
[1] 1 2 3

b)

$a
[1] 1 2 3
$b
[1] 1 2 3

c)

$a
[1] 1 2 3
$b
[1] 1 2

d) Error
View Answer

Answer: a
Explanation: Whether you use an anonymous function or you define a function first depends on your context.

10. What will be the output of the following R code?

> x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
> sapply(x, mean)

a)

a b c d
2.500000 -0.251483 1.481246 4.968715

b)

a b c d
2.500000 -3.251483 2.481246 5.968715

c)

a b c d
3.500000 0.251483 1.481246 4.968715

d) Error
View Answer

Answer: a
Explanation: sapply() collapsed the output into a numeric vector, which is often more useful than a list.

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 for online quizzes, Here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.