R Programming

How to use apply() function in R

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

apply() function in R programming is used to perform a specified function, either a R base installation function or an user-written function, to the rows or columns of a matrix or data frame. The basic for of the function is :

apply(object, axis, fun)

Where object is a matrix or data frame, axis denotes application by rows(1) or columns(2) and fun is a function.

In the following example code, we first create a numerical matrix, then use apply() to calculate mean values for each row and each column of the matrix.

#create a matrix
test <- matrix(rnorm(20), nrow = 4)
test
#output
           [,1]        [,2]       [,3]       [,4]       [,5]
[1,]  0.5313374 -0.06557855  0.1768340 -1.1796457  1.6648249
[2,] -1.3173030  0.90527153  0.2857861  1.2961520 -0.3639763
[3,]  1.8733338 -0.22366606  0.5579957 -0.1023676 -0.2577931
[4,] -0.2645615 -0.96548445 -0.3121663  0.8827860  0.7333012
#to calculate mean of each row, using index '1'
apply(test, 1, mean)
#result
[1] 0.2255544 0.1611860 0.3695006 0.0147750
#calculate mean of each column, using index '2' in apply() function
apply(test, 2, mean) 
#result 
[1]  0.20570166 -0.08736438  0.17711238  0.22423116  0.44408918
#calculate trimmed mean of each column, by excluding lower 20%
#and upper 20% of the observations.
apply(test, 2, mean, trim = 0.2)
#result 
[1]  0.20570166 -0.08736438  0.17711238  0.22423116  0.44408918

Next example show how to calculate sum by rows and columns of a matrix using apply() function.

#create a sample matrix
sample_matrix <- matrix(C<-(1:10),nrow=3, ncol=10)
print( "sample matrix:")
sample_matrix
#output
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    4    7   10    3    6    9    2    5     8
[2,]    2    5    8    1    4    7   10    3    6     9
[3,]    3    6    9    2    5    8    1    4    7    10
# Use apply() function across rows of matrix to find sum
print("sum across rows:")
apply( sample_matrix, 1, sum)
#result
[1] 55 55 55
# use apply() function across columns of matrix to find mean
print("mean across columns:")
apply( sample_matrix, 2, sum)
#result 
[1]  6 15 24 13 12 21 20  9 18 27

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago