Loop Functions
lapply
Loop over a list and apply a function.
Let's calculate mean of each element in the list using mean
function.
Using runif
function.
sapply
sapply
is almost the same as lapply
but it tries to simplify the result if possible.
If the result is a list, it returns vector
If the result is list where each element is vector, matrix is returned
If it does not know, list is returned
Let's see what is the difference between lapply and sapply.
apply
Evaluate
X - is array
MARGIN - margin to retain
FUN - function
... - argumetns for FUN function
We can use
rowSums
,rowMeans
,colSums
andcolMeans
functions instead, they are faster.
mapply
With mapply function we can iterate trough multiple sets. E.g.
tapply
Applies function across subset of a vector. It splits data into little pieces and after calculation it is put together again.
First we create a sample data.
Mean of each number in x.
Turn of simplify and function returns list then.
Last updated