Open In App

Apply function to each column of matrix in R

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore a method to apply the function to each matrix column by using R Programming Language.

How to apply the function to each column of the matrix

The function ‘apply()’ is used to apply the function to each column of the matrix. By using these methods provided by R, it is easy to apply the function. The method to apply the function to each column of the matrix is

This method is used to apply the function to each column of the matrix. The syntax is as follows

apply(X, MARGIN, FUN)

Where:

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

In the below example, we calculated mean of each column by using the function apply().

R
a=c(12,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47)

#creating matrix
mat = matrix(a, nrow=3)
print("original matrix")
print(mat)

print("Applying function to each column for calculating mean")
result = apply(mat, 2, mean)
print(result)

Output:

[1] "original matrix"
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 12 19 25 31 37 43
[2,] 15 21 27 33 39 45
[3,] 17 23 29 35 41 47

[1] "Applying function to each column for calculating mean"
[1] 14.66667 21.00000 27.00000 33.00000 39.00000 45.00000

In the below example, we calculated sum of each column by using the function apply().

R
#creating matrix
mat = matrix(55:74, nrow=5)
print("original matrix")
print(mat)

print("Applying function to each column for calculating sum")
result <- apply(mat, 2, sum)
print(result)

Output:

[1] "original matrix"
[,1] [,2] [,3] [,4]
[1,] 55 60 65 70
[2,] 56 61 66 71
[3,] 57 62 67 72
[4,] 58 63 68 73
[5,] 59 64 69 74

[1] "Applying function to each column for calculating sum"
[1] 285 310 335 360

In the below example, we calculated standard deviation of each column by using the function apply().

R
#creating matrix
matrix = matrix(1:25, nrow=5)
print("original matrix")
print(matrix)

print("Applying function to each column for calculating standard deviation")
result <- apply(matrix, 2, sd)
print(result)

Output:

[1] "original matrix"
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25

[1] "Applying function to each column for calculating standard deviation"
[1] 1.581139 1.581139 1.581139 1.581139 1.581139

In the below example, we multiplied each column of elements with an integer 5 by using the function apply().

R
#creating matrix
matrix = matrix(1:25, nrow=5)
print("original matrix")
print(matrix)

print("Applying function to each column for multiplying")
result <- apply(matrix, 2, function(x) x * 5)
print(result)

Output:

[1] "original matrix"
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25

[1] "Applying function to each column for multiplying"
[,1] [,2] [,3] [,4] [,5]
[1,] 5 30 55 80 105
[2,] 10 35 60 85 110
[3,] 15 40 65 90 115
[4,] 20 45 70 95 120
[5,] 25 50 75 100 125

Conclusion

In Conclusion, we learned about how to apply function to each column of matrix by using the function apply(). R language provides versatile tools and functions while dealing with matrices.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads