apply functions in R

It takes a lot of time to run for loop, apply function is different and based on C language, which saves a lot of time.

#apply family

##apply

##lapply

##sapply

##vapply

##mapply

##tapply

##rapply

##eapply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

tapply; apply
mapply
lapply�>sapply�>vapply
�>rapply
eapply

#apply
apply() can be applied to matrix, dataframe, array
apply(X,MARGIN,FUN,�), X: dataframe,matrix,array; MARGIN: applied to rows when =1, applied to columns when =2; FUN: function that apply to the data

#lapply
apply a given function to every element of a list and obtain a list as result, it can be applied to dataframes, lists or vectors, the output
return to a list
lapply(X,FUN,�)
lapply cannot be applied to vector or matrix

#sapply
similar to lapply, but return to vector, not list
sapply(X,FUN,�,simplify=TRUE,USE.NAMES=TRUE)
X can be array, matrix, dataframe
simplify: whether to array
USE.NAMES: if X is a string, then TRUE set string as the data name
if simplify=FALSE and USE.NAMES=FALSE, then sapply is equal to lapply
if simplify=�array� then can create matrixes
if USE.NAMES=TRUE, then can create data name

#vapply
it is similar to sapply
vapply(X,FUN,FUN.VALUE,�,USE.NAMES=TRUE)