How to create a new column with means of row values for each or some of the columns in an R data frame?


Comparison of columns of an R data frame can be done in many ways and one of the ways is having one or more columns of means. In this way, we can compare column of raw data with the column of means and also the column of means with another column of means. We can use apply function to create a new column with means of row values for each or some of the columns of an R data frame.

Example

Consider the below data frame

x1 <-1:20
x2 <-1:20
x3 <-20:1
df <-data.frame(x1,x2,x3)
df

Output

x1 x2 x3
1 1 1 20
2 2 2 19
3 3 3 18
4 4 4 17
5 5 5 16
6 6 6 15
7 7 7 14
8 8 8 13
9 9 9 12
10 10 10 11
11 11 11 10
12 12 12 9
13 13 13 8
14 14 14 7
15 15 15 6
16 16 16 5
17 17 17 4
18 18 18 3
19 19 19 2
20 20 20 1

We will create new column for the mean of all columns and for the mean of some columns −

Example

df$Means <-apply(df,1,mean)
df

Output

x1 x2 x3 Means
1 1 1 20 7.333333
2 2 2 19 7.666667
3 3 3 18 8.000000
4 4 4 17 8.333333
5 5 5 16 8.666667
6 6 6 15 9.000000
7 7 7 14 9.333333
8 8 8 13 9.666667
9 9 9 12 10.000000
10 10 10 11 10.333333
11 11 11 10 10.666667
12 12 12 9 11.000000
13 13 13 8 11.333333
14 14 14 7 11.666667
15 15 15 6 12.000000
16 16 16 5 12.333333
17 17 17 4 12.666667
18 18 18 3 13.000000
19 19 19 2 13.333333
20 20 20 1 13.666667

Example

df$Means_of_x1_and_x2 <-apply(df[,1:2],1,mean)
df

Output

x1 x2 x3 Means Means_of_x1_and_x2
1 1 1 20 7.333333 1
2 2 2 19 7.666667 2
3 3 3 18 8.000000 3
4 4 4 17 8.333333 4
5 5 5 16 8.666667 5
6 6 6 15 9.000000 6
7 7 7 14 9.333333 7
8 8 8 13 9.666667 8
9 9 9 12 10.000000 9
10 10 10 11 10.333333 10
11 11 11 10 10.666667 11
12 12 12 9 11.000000 12
13 13 13 8 11.333333 13
14 14 14 7 11.666667 14
15 15 15 6 12.000000 15
16 16 16 5 12.333333 16
17 17 17 4 12.666667 17
18 18 18 3 13.000000 18
19 19 19 2 13.333333 19
20 20 20 1 13.666667 20

Example

df$Means_of_x1_and_x3 <-apply(df[,1:3],1,mean)
df

Output

x1 x2 x3 Means Means_of_x1_and_x2 Means_of_x1_and_x3
1 1 1 20 7.333333 1 7.333333
2 2 2 19 7.666667 2 7.666667
3 3 3 18 8.000000 3 8.000000
4 4 4 17 8.333333 4 8.333333
5 5 5 16 8.666667 5 8.666667
6 6 6 15 9.000000 6 9.000000
7 7 7 14 9.333333 7 9.333333
8 8 8 13 9.666667 8 9.666667
9 9 9 12 10.000000 9 10.000000
10 10 10 11 10.333333 10 10.333333
11 11 11 10 10.666667 11 10.666667
12 12 12 9 11.000000 12 11.000000
13 13 13 8 11.333333 13 11.333333
14 14 14 7 11.666667 14 11.666667
15 15 15 6 12.000000 15 12.000000
16 16 16 5 12.333333 16 12.333333
17 17 17 4 12.666667 17 12.666667
18 18 18 3 13.000000 18 13.000000
19 19 19 2 13.333333 19 13.333333
20 20 20 1 13.666667 20 13.666667

Example

$Means_of_x2_and_x3 &klt;-apply(df[,2:3],1,mean)
df

Output

x1 x2 x3 Means Means_of_x1_and_x2 Means_of_x1_and_x3 Means_of_x2_and_x3
1 1 1 20 7.333333 1 7.333333 10.5
2 2 2 19 7.666667 2 7.666667 10.5
3 3 3 18 8.000000 3 8.000000 10.5
4 4 4 17 8.333333 4 8.333333 10.5
5 5 5 16 8.666667 5 8.666667 10.5
6 6 6 15 9.000000 6 9.000000 10.5
7 7 7 14 9.333333 7 9.333333 10.5
8 8 8 13 9.666667 8 9.666667 10.5
9 9 9 12 10.000000 9 10.000000 10.5
10 10 10 11 10.333333 10 10.333333 10.5
11 11 11 10 10.666667 11 10.666667 10.5
12 12 12 9 11.000000 12 11.000000 10.5
13 13 13 8 11.333333 13 11.333333 10.5
14 14 14 7 11.666667 14 11.666667 10.5
15 15 15 6 12.000000 15 12.000000 10.5
16 16 16 5 12.333333 16 12.333333 10.5
17 17 17 4 12.666667 17 12.666667 10.5
18 18 18 3 13.000000 18 13.000000 10.5
19 19 19 2 13.333333 19 13.333333 10.5
20 20 20 1 13.666667 20 13.666667 10.5

Updated on: 21-Aug-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements