R Programming Quizzes

apply() functions

Question 1

Which function is used to apply a function (e.g., sum or mean) over the margins (rows or columns) of a matrix or array?

Question 2

The function that applies a given function to each element of a list and is guaranteed to return a list as its output is _____().

Your answer here.

Question 3

You have a data frame iris and want to calculate the mean Sepal.Length for each Species. Which function is most appropriate for this task?

Question 4

What is the primary difference between lapply() and its wrapper sapply()?

Question 5

You have a 5x10 matrix M. Which of the following apply calls will compute the mean of each row?

Question 6

Which function is best suited for applying a function to parallel elements of multiple vectors? (e.g., applying a function to the 1st elements of vector A and B, then the 2nd elements, etc.)

Question 7

Which of the following functions from the apply family are commonly used in R?

Question 8

The apply family of functions provides a more concise and often more efficient alternative to writing explicit _____-loops in R.

Your answer here.

End of Quiz

Your final score is: 0 / 0

List of Questions

Question 1: Which function is used to apply a function (e.g., `sum` or `mean`) over the margins (rows or columns) of a matrix or array?

Answers: apply().

Question 2: You have a data frame `iris` and want to calculate the mean `Sepal.Length` for each `Species`. Which function is most appropriate for this task?

Answers: tapply(iris$Sepal.Length, iris$Species, mean).

Question 3: What is the primary difference between `lapply()` and its wrapper `sapply()`?

Answers: `lapply()` returns a list, while `sapply()` attempts to simplify the result into a vector or matrix if possible..

Question 4: You have a 5x10 matrix `M`. Which of the following `apply` calls will compute the mean of each row?

Answers: apply(M, 1, mean).

Question 5: Which function is best suited for applying a function to parallel elements of multiple vectors? (e.g., applying a function to the 1st elements of vector A and B, then the 2nd elements, etc.)

Answers: mapply().

Question 6: Which of the following functions from the apply family are commonly used in R?

Answers: apply(), lapply(), tapply(), mapply().