0% found this document useful (0 votes)
27 views

Apply Functions

Apply functions allow repetitive operations on data chunks in R. Functions like apply(), lapply(), sapply(), mapply(), tapply(), and rapply() operate on different data types like matrices, data frames, lists, and vectors, returning results in vectors, lists or arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Apply Functions

Apply functions allow repetitive operations on data chunks in R. Functions like apply(), lapply(), sapply(), mapply(), tapply(), and rapply() operate on different data types like matrices, data frames, lists, and vectors, returning results in vectors, lists or arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Apply Functions

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)

• Where the first Argument X is a data frame or matrix

• Second argument 1 indicated Processing along rows .if it is


2 then it indicated processing along the columns

• Third Argument is some aggregate function like sum, mean


etc or some other user defined functions.
 
Example
English<-c(56,34,67,33,25,28)
Maths<-c(78,67,56,44,56,89)
Science<-c(65, 71,67,67,66,81)
My_Data<-data.frame(English,Maths,Science)
My_Data
Example
• apply(My_Data,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)

• The lapply function applies mean function to


the columns of the dataframe.  
sapply function
 sapply function takes list, vector or Data
frame  as input.
 It is similar to lapply function but returns only
vector as output.
Example
• sapply(My_Data, function(My_Data)
My_Data/2)
• The sapply function divides the values in the
dataframe by 2.
Example
• sapply(BMI_df, mean)

• The sapply function applies mean function to


the columns of the dataframe and the output
will be in the form of vector.  
Example
• My_Data_1 <-
c("This","is","random","vector")
• sapply(My_Data_1,nchar)
mapply function
 mapply is a multivariate version of sapply. mapply
applies FUN to the first elements of each (…) argument,
the second elements, the third elements, and so on.
• i.e. For when you have several data structures (e.g.
vectors, lists) and you want to apply a function to the
1st elements of each, and then the 2nd elements of
each, etc., coercing the result to a vector/array as
in sapply
• This is multivariate in the sense that your function must
accept multiple arguments.
Examples
1. mapply(sum, 1:4, 1:4, 1:4)
mapply sums up all the first
elements(1+1+1) ,sums up all the, second
elements(2+2+2) and so on.

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"))

Remark: rapply function is applied even for the


sublists.
vapply

• vapply function in R is similar to sapply, but


has a pre-specified type of return value, so it
can be safer (and sometimes faster) to use.
Example
# vapply function in R
vapply(1:5, sqrt, 1i)

You might also like