Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Raju N
Apply functions
• Apply functions are a family of functions in base R which allow us
to repetitively perform an action on multiple chunks of data.
• An apply function is essentially a loop, but run faster than loops
and often require less code.
• The apply functions that discussed here are apply, lapply, sapply,
vapply, tapply, and mapply.
• There are so many different apply functions because they are
meant to operate on different types of data.
• apply() takes Data frame or matrix as an input and gives output in
vector, list or array.
• apply() Function is primarily used to avoid explicit uses of loop
constructs.
apply function
Returns a vector or array or list of values
obtained by applying a function to margins of
an array or matrix.
• Syntax for Apply function in R:
• apply(x,1,sum)
Syntax - apply
• apply(x,1,sum)
• apply(My_Data,2,sum)
• apply(My_Data,2,mean)
lapply function
• lapply function takes list, vector or Data frame
as input and returns only list as output.
• lapply() function is useful for performing
operations on list objects and returns a list
object of same length of original set.
• lappy() returns a list of the similar length as
input list object, each element of which is the
result of applying FUN to the corresponding
element of list.
lapply
• lapply(My_Data, mean)
2. mapply(rep,1:4,1:4)
it repeats the first element once , second
element twice and so on.
tapply function
When we want to apply a function to subsets of
a vector and the subsets are defined by some
other vector, usually a factor.
• Let us consider famous iris data. Species is a
factor with 3 values namely Setosa,
versicolor and virginica.
• If we want to find the mean of sepal length of
these 3 species(subsets). we can use tapply
function
Example
# tapply function in R
attach(iris)
# mean sepal length by species
tapply(iris$Sepal.Length, Species, mean)
Remark: First argument of tapply function takes the
vector for which we need to perform the function.
second argument is a vector by which we need to
perform the function and third argument is the
function, here it is mean.
• tapply() function
• tapply() computes a measure (mean, median,
min, max, etc..) or a function for each factor
variable in a vector. It is a very useful function
that lets you create a subset of a vector and
then apply some functions to each of the
subset.
rapply
• rapply function in R is nothing but recursive
apply, as the name suggests it is used to apply
a function to all elements of a list recursively.
Example
• # rapply function in R
• x=list(1,2,3,4)
• rapply(x,function(x){x^2},class=c("numeric"))
Contd…
• first argument in the rapply function is the list,
here it is x.
• the second argument is the function that
needs to be applied over the list.
• last argument gives the classes to which the
function should be applied
x=list(3,list(4,5),6,list(7,list(8,9)))
str(x)
rapply(x,function(x) x^2,class=c("numeric"))