The apply family Flashcards

(5 cards)

1
Q

Which function turns a list into a vector?

A

unlist()

Ex: unlist(my_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which function applies the given function to every element of the inputted vector or list, returning a list?

A

lapply()

Ex: lapply(my_vector, my_func)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which function applies the inputted function to every element of the inputted vector, before simplifying the result if possible into a vector or matrix?

A

sapply()

Ex: sapply(my_vector, my_func)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which function applies the inputted function to every element of the inputted vector, before simplifying the result, requiring the format of the inputted function to be explicitly inputted?

A

vapply()

Ex: vapply(my_vector, max, numeric(1))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which function is safer, vapply or sapply?

A

vapply is safer because by forcing you to specify the return type of the inputted function, vapply essentially forces you to think about whether the output will ultimately be a vector, matrix, or list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly