lapply is used to show the output in the form of list whereas sapply is used to show the output in the form of vector or data frame.
lapply returns a list of the same length as X , each element of which is the result of applying FUN to the corresponding element of X . sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify = “array” , an array if appropriate, by applying simplify2array() .
It is useful for operations on list objects and returns a list object of same length of original set. Sapply function in R does the same job as lapply() function but returns a vector. … Sapply in R is more efficient than lapply() in the output returned because sapply() store values direclty into a vector.
Function | Arguments | Objective | Input | Output |
---|---|---|---|---|
apply | apply(x, MARGIN, FUN) | Apply a function to the rows or columns or both | Data frame or matrix | vector, list, array |
lapply | lapply(X, FUN) | Apply a function to all the elements of the input | List, vector or data frame | list |
sapply | sapply(X, FUN) | Apply a function to all the elements of the input | List, vector or data frame | vector or matrix |