The apply family Flashcards
(5 cards)
Which function turns a list into a vector?
unlist()
Ex: unlist(my_list)
Which function applies the given function to every element of the inputted vector or list, returning a list?
lapply()
Ex: lapply(my_vector, my_func)
Which function applies the inputted function to every element of the inputted vector, before simplifying the result if possible into a vector or matrix?
sapply()
Ex: sapply(my_vector, my_func)
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?
vapply()
Ex: vapply(my_vector, max, numeric(1))
Which function is safer, vapply or sapply?
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.