0 votes
in R Language by

apply(), lapply(), sapply(), tapply() Function in R with Examples

1 Answer

0 votes
by

apply() function

We use apply() over a matrice. This function takes 5 arguments:

apply(X, MARGIN, FUN)
Here:
-x: an array or matrix
-MARGIN:  take a value or range between 1 and 2 to define where to apply the function:
-MARGIN=1`: the manipulation is performed on rows
-MARGIN=2`: the manipulation is performed on columns
-MARGIN=c(1,2)` the manipulation is performed on rows and columns
-FUN: tells which function to apply. Built functions like mean, median, sum, min, max and even user-defined functions can be applied>

lapply() function

lapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element of x	

l in lapply() stands for list. The difference between lapply() and apply() lies between the output return. The output of lapply() is a list. lapply() can be used for other objects like data frames and lists.

lapply() function does not need MARGIN.

sapply() function

sapply() function does the same jobs as lapply() function but returns a vector.

sapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element of x

Related questions

0 votes
asked Nov 14, 2019 in R Language by MBarbieri
0 votes
asked Nov 6, 2019 in R Language by MBarbieri
...