How to find the average of a particular column in R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the average of a particular column in R data frame, we can take the help of delta ($) operator.

For example, if we have a data frame called df that contains a column x then we can find the average of column x by using the command given below −

mean(df$x)

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created −

       x1           x2           x3
1   0.08296609  -0.98030681  -0.37284625
2  -0.77756287  -2.00417903   0.99687606
3   0.72853715   0.01054841   0.21984261
4   0.32172853  -0.15294023   0.81413538
5   0.14037370  -1.72069152  -0.40727802
6  -0.46377087  -0.44384035  -0.27233178
7  -0.07626076  -0.55753570   0.09087911
8  -0.15585465   1.02326012   0.59296301
9   0.36429494   0.48666242  -0.29251297
10 -1.71607624  -1.14980411  -0.52830887
11 -0.13649599  -0.48478367   0.57505756
12  0.20013689  -1.07749624   1.78867277
13 -0.78971636  -1.35245763   0.03213526
14 -1.08909636  -0.58043334  -0.32440015
15 -0.16291871   0.08974447   0.64379662
16 -0.16230288  -0.27600414   0.09700218
17 -0.71638476   0.36130502   1.63317291
18  0.22151476   0.95380907  -0.12504823
19 -1.72743039  -1.01398759   1.05922322
20  0.11511031   0.09696002   0.90670155

To find the mean of column x1, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
mean(df1$x1)

If you execute all the above given snippets as a single program, it generates the following output −

[1] -0.2899604

To find the mean of column x2, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
mean(df1$x2)

If you execute all the above given snippets as a single program, it generates the following output −

[1] -0.4386085

To find the mean of column x3, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
mean(df1$x3)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 0.3563866

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,2)
y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
df2

The following dataframe is created −

   y1 y2 y3 y4
1  0  3  2  2
2  4  3  1  4
3  0  6  1  9
4  2  2  0  5
5  2  3  0  5
6  2  6  2  6
7  0  4  0  3
8  2  7  0  1
9  3  5  2  2
10 3  4  1  4
11 0  6  2  3
12 1  4  1  4
13 3  4  0  7
14 1  2  2 10
15 2  4  0  5
16 4  4  2  4
17 1  3  0  5
18 0  4  3  7
19 2  4  1  6
20 2  3  0  6

To find the mean of column y1, add the following code to the above snippet −

y1<-rpois(20,2)
y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
mean(df2$y1)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 1.7

To find the mean of column y2, add the following code to the above snippet −

y1<-rpois(20,2)
y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
mean(df2$y2)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 4.05

To find the mean of column y3, add the following code to the above snippet −

y1<-rpois(20,2)
y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
mean(df2$y3)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 1

To find the mean of column y4, add the following code to the above snippet −

y1<-rpois(20,2)
y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
mean(df2$y4)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 4.9

Example 3

Following snippet creates a sample data frame −

z1<-rexp(20)
z2<-rexp(20)
z3<-rexp(20)
df3<-data.frame(z1,z2,z3)
df3

The following dataframe is created −

        z1         z2        z3
1  0.75901140  1.1019604  1.857275549
2  0.70534396  0.5027629  0.141321053
3  0.85023827  0.3935630  1.096067266
4  0.79056207  0.3866263  0.004878585
5  0.02140214  0.5251550  1.255923276
6  2.10659256  1.2074068  1.366982480
7  0.62689870  1.0928418  0.127860267
8  0.42471756  0.7221201  0.804941206
9  0.20822713  0.4138671  1.206996112
10 0.43427296  2.2273157  0.448053845
11 0.20142638  0.6122972  0.827769115
12 1.11057268  0.6116563  2.259519266
13 2.45655773  1.1361731  2.467388572
14 0.07043767  4.6228876  2.722874785
15 1.16999613  0.2022302  2.775020942
16 1.27551362  0.1391906  2.669347687
17 0.17801680  1.3281677  1.686527666
18 1.16893835  0.7579054  0.872252452
19 0.24239762  1.3178056  1.207718495
20 0.72186035  4.3268620  1.504209600

To find the mean of column z1, add the following code to the above snippet −

z1<-rexp(20)
z2<-rexp(20)
z3<-rexp(20)
df3<-data.frame(z1,z2,z3)
mean(df3$z1)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 0.7761492

To find the mean of column z2, add the following code to the above snippet −

z1<-rexp(20)
z2<-rexp(20)
z3<-rexp(20)
df3<-data.frame(z1,z2,z3)
mean(df3$z2)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 1.18144

To find the mean of column z3, add the following code to the above snippet −

z1<-rexp(20)
z2<-rexp(20)
z3<-rexp(20)
df3<-data.frame(z1,z2,z3)
mean(df3$z3)

If you execute all the above given snippets as a single program, it generates the following output −

[1] 1.365146
raja
Published on 22-Nov-2021 07:12:53
Advertisements