How to create a new data frame for the mean of rows of some columns from an R data frame?

Finding row means help us to identity the average performance of a case if all the variables are of same nature and it is also an easy job. But if some of the columns have different type of data then we have to extract columns for which we want to find the row means. Therefore, we can create a new data frame with row means of the required columns using rowMeans function.

Example

row_means_3.4_cols_df<-data.frame(Group=df[,1],RowMeans=rowMeans(df[,-c(1,2)]))
row_means_3.4_cols_df
Group RowMeans
1 A 6.5
2 B 6.5
3 C 8.0
4 D 8.5
5 E 8.5
6 F 5.0
7 G 4.5
8 H 8.5
9 I 8.0
10 J 7.0
11 A 6.0
12 B 7.5
13 C 6.0
14 D 5.5
15 E 9.5
16 F 8.5
17 G 8.0
18 H 7.5
19 I 6.0
20 J 7.5
Updated on: 2026-03-11T23:22:53+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements