How to find the mean of all values in an R data frame?


If a data frame has all numerical columns then we might be interested in finding the mean of all values in that data frame but this cannot be done directly because a data frame object is not numeric. Therefore, to find the mean of all values in an R data frame, we need to convert it to a matrix first then use the mean function.

Example

Consider the below data frame −

 Live Demo

x1<-rnorm(20,0.5)
x2<-rpois(20,2)
x3<-rpois(20,5)
x4<-rpois(20,10)
x5<-rnorm(20,2)
df1<-data.frame(x1,x2,x3,x4,x5)
df1

Output

     x1       x2  x3   x4    x5
1 0.26964555   1   3    5    1.016116
2 0.42291565   1   2   11    3.128720
3 0.65188853   2   8   13    2.084747
4 1.36720540   0   8    4    1.796721
5 0.71275824   0   4    12    3.689740
6 0.40522303   3   5    12    3.479856
7 1.24929901   0   6     6    1.819096
8 0.16090460   5   7    11    2.097494
9 1.59639183   3   3    10    1.975716
10 0.58234446  4   3    11    3.249315
11 0.32498206  2   5    6    1.574391
12 1.86227329  3   7    11    1.364608
13 0.57444839  3   4    7    2.587213
14 1.71832408  0   4    8    2.623389
15 0.06276699  2   5    9    2.931708
16 -0.21100119 1   10   13    2.975824
17 1.85209226  1   4    13    2.616432
18 0.25671284  1   5    11    2.480839
19 -0.52819876 1   5    11    3.080255
20 -0.19347216 1   5    8    1.556553

Finding the mean using mean function −

Example

mean(df1)

Output

[1] NA

Warning message −

In mean.default(df1) − argument is not numeric or logical − returning NA

Here, it is showing a warning message, therefore, we need to convert the data frame to a matrix for mean calculation −

Example

mean(as.matrix(df1))

Output

[1] 3.902662

Let’s have a look at another example −

Example

 Live Demo

y1<-runif(20,1,2)
y2<-runif(20,2,3)
y3<-runif(20,2,4)
y4<-runif(20,2,5)
y5<-runif(20,2,10)
y6<-runif(20,5,10)
df2<-data.frame(y1,y2,y3,y4,y5,y6)
df2

Output

      y1       y2       y3       y4       y5       y6
1 1.368129 2.738308 3.161148 4.046335 6.779294 6.587224
2 1.476344 2.916177 3.772860 3.781855 4.180840 5.689949
3 1.048968 2.557214 2.616399 4.544586 5.332783 7.590678
4 1.237608 2.886140 3.566979 2.233891 7.110491 5.400549
5 1.052892 2.085286 3.929607 3.001106 8.221596 8.019978
6 1.978942 2.558619 3.558014 2.275578 6.169379 9.002451
7 1.800132 2.692973 2.329893 4.700417 9.436470 5.828630
8 1.362224 2.647957 2.361931 4.209640 9.838114 8.874549
9 1.379799 2.521889 3.516044 4.751619 6.102215 7.123205
10 1.682376 2.854172 2.507239 2.605997 7.708206 7.139015
11 1.652283 2.546204 3.005126 2.188606 3.573105 7.826812
12 1.839336 2.663233 2.644623 3.820956 9.414881 8.410358
13 1.027927 2.741457 3.997138 2.454044 9.394880 7.838431
14 1.539522 2.790478 2.751248 2.446984 9.048925 5.909314
15 1.112645 2.582921 2.028874 4.003921 5.382478 6.043791
16 1.038845 2.819239 3.011070 2.966783 8.145579 7.155915
17 1.140283 2.600742 3.867859 2.332695 7.513786 7.659354
18 1.641807 2.601332 3.274624 4.081097 9.738734 5.018204
19 1.790096 2.839944 3.250313 3.507385 7.654682 7.040064
20 1.377599 2.105186 2.383566 2.610954 2.762618 7.352752

Example

mean(as.matrix(df2))

Output

[1] 4.120138

Updated on: 08-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements