How to Apply Function to Each Row in Matrix or Data Frame in R


You can use the apply() function to apply a function to each row in a matrix or data frame in R.

This function uses the following basic syntax:

apply(X, MARGIN, FUN)

where:

  • X: Name of the matrix or data frame.
  • MARGIN: Dimension to perform operation across. Use 1 for row, 2 for column.
  • FUN: The function to apply.

The following examples show how to use this syntax in practice.

Example 1: Apply Function to Each Row in Matrix

Suppose we have the following matrix in R:

#create matrix
mat <- matrix(1:15, nrow=3)

#view matrix
mat

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15

We can use the apply() function to apply different functions to the rows of the matrix:

#find mean of each row
apply(mat, 1, mean)

[1] 7 8 9

#find sum of each row
apply(mat, 1, sum)

[1] 35 40 45

#find standard deviation of each row
apply(mat, 1, sd)

[1] 4.743416 4.743416 4.743416

#multiply the value in each row by 2 (using t() to transpose the results)
t(apply(mat, 1, function(x) x * 2))

     [,1] [,2] [,3] [,4] [,5]
[1,]    2    8   14   20   26
[2,]    4   10   16   22   28
[3,]    6   12   18   24   30

#normalize every row to 1 (using t() to transpose the results)
t(apply(mat, 1, function(x) x / sum(x) ))

           [,1]      [,2] [,3]      [,4]      [,5]
[1,] 0.02857143 0.1142857  0.2 0.2857143 0.3714286
[2,] 0.05000000 0.1250000  0.2 0.2750000 0.3500000
[3,] 0.06666667 0.1333333  0.2 0.2666667 0.3333333

Note that if you’d like to find the mean or sum of each row, it’s faster to use the built-in rowMeans() or rowSums() functions:

#find mean of each row
rowMeans(mat)

[1] 7 8 9

#find sum of each row
rowSums(mat)

[1] 35 40 45

Example 2: Apply Function to Each Row in Data Frame

Suppose we have the following matrix in R:

#create data frame
df <- data.frame(var1=1:3,
                 var2=4:6,
                 var3=7:9,
                 var4=10:12,
                 var5=13:15)

#view data frame
df

  var1 var2 var3 var4 var5
1    1    4    7   10   13
2    2    5    8   11   14
3    3    6    9   12   15

We can use the apply() function to apply different functions to the rows of the data frame:

#find mean of each row
apply(df, 1, mean)

[1] 7 8 9

#find sum of each row
apply(df, 1, sum)

[1] 35 40 45

#find standard deviation of each row
apply(df, 1, sd)

[1] 4.743416 4.743416 4.743416

#multiply the value in each row by 2 (using t() to transpose the results)
t(apply(df, 1, function(x) x * 2))

     var1 var2 var3 var4 var5
[1,]    2    8   14   20   26
[2,]    4   10   16   22   28
[3,]    6   12   18   24   30

#normalize every row to 1 (using t() to transpose the results)
t(apply(df, 1, function(x) x / sum(x) ))

           var1      var2 var3      var4      var5
[1,] 0.02857143 0.1142857  0.2 0.2857143 0.3714286
[2,] 0.05000000 0.1250000  0.2 0.2750000 0.3500000
[3,] 0.06666667 0.1333333  0.2 0.2666667 0.3333333

Similar to matrices, if you’d like to find the mean or sum of each row, it’s faster to use the built-in rowMeans() or rowSums() functions:

#find mean of each row
rowMeans(df)

[1] 7 8 9

#find sum of each row
rowSums(df)

[1] 35 40 45

Additional Resources

How to Retrieve Row Numbers in R
How to Perform a COUNTIF Function in R
How to Perform a SUMIF Function in R

Leave a Reply

Your email address will not be published.