How to find the mean of columns of an R data frame or a matrix?

R ProgrammingServer Side ProgrammingProgramming

If all the columns in an R data frame are numeric then it makes sense to find the mean for each of the columns. This calculation will help us to view how different the values of means are for each of the columns but to make sure that they are significantly different, we will need to run a hypothesis test. To find the column means of a data frame or a matrix we can use colMeans function.

Example

Consider the below data frame −

Live Demo

set.seed(9)
x1<-rnorm(20,0.2)
x2<-rnorm(20,0.5)
x3<-rnorm(20,0.8)
x4<-rnorm(20,1.5)
x5<-rpois(20,2)
x6<-rpois(20,5)
df<-data.frame(x1,x2,x3,x4,x5,x6)
df

Output

x1 x2 x3 x4 x5 x6
1  -0.56679604 2.2569929 -0.008456344 1.7812222 2 4
2  -0.61645834 0.6822521 -1.219381694 0.2972914 4 5
3   0.05846481 0.2331113 0.061275928 1.9651637 1 3
4  -0.07760503 1.4264216 1.182886561 1.8520164 4 5
5   0.63630690 -0.1933319 2.530863668 0.9101438 2 3
6  -0.98687252 3.1819901 0.596918049 0.6464000 4 10
7   1.39198691 0.7225245 -0.196397348 1.2532679 4 7
8   0.18180966 -0.2066724 -0.506536295 3.0393386 3 5
9  -0.04808460 0.9172132 -0.197831604 2.0460777 0 4
10 -0.16293689 0.8695568 0.234971274 3.0649619 1 3
11 1.47757055 -0.4137643 2.552057836 1.7496702 2 4
12 -0.26889715 0.1830853 0.252228648 -0.4624186 1 4
13 0.27105410 1.5490592 -0.058525708 0.6909398 3 3
14 -0.06603845 0.6681118 0.849294533 1.0013149 2 5
15 2.04525720 0.5314402 0.599955518 1.8051218 1 7
16 -0.63944966 -0.5103305 -0.303954449 1.2107928 0 4
17 0.12255194 0.8827515 1.040588038 2.9577142 0 2
18 -2.41770553 -0.3196965 1.181113616 2.3737555 5 5
19 1.08788403 0.8617111 3.030458950 0.5470440 3 6
20 -0.50749145 0.5933714 1.999202392 1.4683245 1 6
colMeans(df)
x1 x2 x3 x4 x5 x6
0.04572752 0.69578987 0.68103658 1.50990712 2.15000000 4.75000000

Example

Finding the mean of columns of a matrix if the matrix is a square matrix −

Live Demo

Matrix<-matrix(1:100,nrow=10)
Matrix

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
colMeans(Matrix)
[1] 5.5 15.5 25.5 35.5 45.5 55.5 65.5 75.5 85.5 95.5

Example

Live Demo

Finding the mean of columns of a matrix if the matrix is not a square matrix −

Matrix_new<-matrix(1:100,ncol=20)
Matrix_new

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 1 6 11 16 21 26 31 36 41 46 51 56 61 66
[2,] 2 7 12 17 22 27 32 37 42 47 52 57 62 67
[3,] 3 8 13 18 23 28 33 38 43 48 53 58 63 68
[4,] 4 9 14 19 24 29 34 39 44 49 54 59 64 69
[5,] 5 10 15 20 25 30 35 40 45 50 55 60 65 70
[,15] [,16] [,17] [,18] [,19] [,20]
[1,] 71 76 81 86 91 96
[2,] 72 77 82 87 92 97
[3,] 73 78 83 88 93 98
[4,] 74 79 84 89 94 99
[5,] 75 80 85 90 95 100
colMeans(Matrix_new)
[1] 3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88 93 98
Published on 21-Aug-2020 10:35:46