How to find the row mean for selected columns in R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the row mean for selected columns in R data frame, we can use mutate function of dplyr package along with rowMeans function.

For Example, if we have a data frame called df that contains three columns say X, Y, and Z then mean of each row for columns X and Y can be found by using the below command −

df %% mutate(X_Y_Mean=select(.,c("X","Y")) %% rowMeans())

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,1)
x2<-rpois(20,2)
x3<-rpois(20,2)
x4<-rpois(20,5)
df1<-data.frame(x1,x2,x3,x4)
df1

The following dataframe is created

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

To load dplyr package and find the row means for columns x1 and x2 on the above created data frame, add the following code to the above snippet −

x1<-rpois(20,1)
x2<-rpois(20,2)
x3<-rpois(20,2)
x4<-rpois(20,5)
df1<-data.frame(x1,x2,x3,x4)
library(dplyr)
df1 %% mutate(X1_X2_Mean=select(.,c("x1","x2")) %% rowMeans())

Output

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

  x1 x2 x3 x4 X1_X2_Mean
1  1  1  0  2   1.0
2  2  1  4  7   1.5
3  1  0  3  4   0.5
4  2  2  3  2   2.0
5  2  1  1  5   1.5
6  0  3  1  5   1.5
7  0  2  6  5   1.0
8  0  3  5  7   1.5
9  1  2  2  4   1.5
10 3  1  3  7   2.0
11 1  1  1  4   1.0
12 3  2  0  7   2.5
13 0  2  1  3   1.0
14 1  5  3  3   3.0
15 1  2  0  4   1.5
16 0  3  1  6   1.5
17 0  2  3  4   1.0
18 3  0  1  3   1.5
19 0  3  3  1   1.5
20 2  2  2  1   2.0

Example 2

Following snippet creates a sample data frame −

y1<-round(rnorm(20),2)
y2<-round(rnorm(20),2)
y3<-round(rnorm(20),2)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

     y1    y2 y3
1  -1.77  0.25 -0.85
2   0.38 -0.53  0.01
3  -0.87  0.56  0.06
4   0.37  1.55  0.44
5  -0.01  0.29 -0.35
6  -1.22 -0.40  1.55
7  -0.51 -0.28 -0.04
8   0.24  1.85  0.60
9   0.50  0.50 -0.38
10  0.97  1.06  1.98
11  0.88 -0.83 -1.00
12 -1.31  0.56 -1.06
13  0.41 -0.28  1.57
14 -1.66 -1.30  0.56
15 -2.15 -0.47  0.84
16 -0.27 -2.58 -0.44
17 -2.45  0.90 -2.49
18 -1.82 -1.39 -0.31
19  0.15  0.19 -0.70
20  1.00  0.97  0.05

To find the row means for columns y2 and y3 on the above created data frame, add the following code to the above snippet −

y1<-round(rnorm(20),2)
y2<-round(rnorm(20),2)
y3<-round(rnorm(20),2)
df2<-data.frame(y1,y2,y3)
df2 %% mutate(y2_y3_Mean=select(.,c("y2","y3")) %% rowMeans())

Output

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

     y1   y2    y3  y2_y3_Mean
1  -1.77  0.25 -0.85 -0.300
2   0.38 -0.53  0.01 -0.260
3  -0.87  0.56  0.06  0.310
4   0.37  1.55  0.44  0.995
5  -0.01  0.29 -0.35 -0.030
6  -1.22 -0.40  1.55  0.575
7  -0.51 -0.28 -0.04 -0.160
8   0.24  1.85  0.60  1.225
9   0.50  0.50 -0.38  0.060
10  0.97  1.06  1.98  1.520
11  0.88 -0.83 -1.00 -0.915
12 -1.31  0.56 -1.06 -0.250
13  0.41 -0.28  1.57  0.645
14 -1.66 -1.30  0.56 -0.370
15 -2.15 -0.47  0.84  0.185
16 -0.27 -2.58 -0.44 -1.510
17 -2.45  0.90 -2.49 -0.795
18 -1.82 -1.39 -0.31 -0.850
19  0.15  0.19 -0.70 -0.255
20  1.00  0.97  0.05  0.510
raja
Published on 01-Nov-2021 09:49:14
Advertisements