Syntax error How to find the mean of all columns by group in R?

How to find the mean of all columns by group in R?



To find the mean of all columns by group, we can use summarise_all function along with mean function after defining the groups with group_by. For example, if we have a data frame called df that contains a grouping column say G and some numerical columns then we can find the mean of all columns by values in grouping column by using the below given command −

df%>%group_by(G)%>%summarise_all("mean")

Example 1

Following snippet creates a sample data frame −

Grp<-sample(LETTERS[1:4],20,replace=TRUE)
Score1<-sample(1:50,20)
Score2<-sample(1:50,20)
Score3<-sample(1:50,20)
df1<-data.frame(Grp,Score1,Score2,Score3)
df1

The following dataframe is created −

  Grp Score1 Score2 Score3
1  C   2     1      46
2  C  39    25      21
3  C  25     6      10
4  A   5     8      25
5  A  37    23      17
6  D   4    21      39
7  B  33    40      31
8  C  28    44       3
9  B  40    50       8
10 B  10    49      14
11 B  27    45      42
12 B  45     7       5
13 C  26    34      28
14 C  31    19      12
15 A  29    46      38
16 A  24    38      48
17 A  21    12      22
18 B   1     9      35
19 A  23    20      20
20 B  50    27       9

To load dplyr package and find the mean of all columns by Grp column, add the following code to the above snippet −

library(dplyr)
df1%>%group_by(Grp)%>%summarise_all("mean")
# A tibble: 4 x 4

Outpu

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

   Grp  Score1 Score2 Score3
  <chr> <dbl>  <dbl> <dbl>
1   A   23.2   24.5  28.3
2   B   29.4   32.4  20.6
3   C   25.2   21.5    20
4   D      4     21    39

Example 2

Following snippet creates a sample data frame −

Level<-sample(c("First","Second","Third"),20,replace=TRUE)
Price1<-rpois(20,8)
Price2<-rpois(20,8)
Price3<-rpois(20,8)
df2<-data.frame(Level,Price1,Price2,Price3)
df2

The following dataframe is created −

   Level Price1 Price2 Price3
1  First   11    12     5
2  First    8     6     5
3  First    5    16     6
4  Second   9    11     9
5  Second  12    12    11
6  Second   5    10     8
7  First    7     6     9
8  First   10     6    11
9  Second   4     9     8
10 First    5     9     5
11 Third   14     6     8
12 Third    4    13    11
13 Second   3     8     5
14 Second   8     3     8
15 Second   6    10    10
16 Second  10     3     9
17 First    8     8     5
18 First    6     3    11
19 Third    5    10     9
20 Third   10    11    10

To find the mean of all columns by Level column, add the following code to the above snippet −

df2%>%group_by(Level)%>%summarise_all("mean")
# A tibble: 3 x 4

Output

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

  Level  Price1 Price2 Price3
  <chr>  <dbl>  <dbl> <dbl>
1 First   7.5   8.25  7.12
2 Second  7.12  8.25  8.5
3 Third   8.25    10  9.5
Updated on: 2021-11-11T05:28:13+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements