# How to find the column means by factor levels in R?

R ProgrammingServer Side ProgrammingProgramming

To find the column means by factor levels, we can use summarise function along with mean function after creating the group of factor levels with group_by function.

For example, if we have a data frame called df that contains a factor column say F and a numerical column say Num then we can find the mean of Num column by factor levels by using the below given command −

df%>%group_by(F)%>%summarise(Average=mean(Num))

## Example 1

Following snippet creates a sample data frame −

grp<-sample(LETTERS[1:4],20,replace=TRUE)
response<-rpois(20,5)
df1<-data.frame(grp,response)
df1

The following dataframe is created −

 grp response
1  A  5
2  B  3
3  A  4
4  D  6
5  A  7
6  A  5
7  B  2
8  A  3
9  A  7
10 C  1
11 B  9
12 B  5
13 D  3
14 B  5
15 D  1
16 A  9
17 D  9
18 C  7
19 D  6
20 B  3

To load dplyr package and find the mean of response by factor levels in column grp, add the following code to the above snippet −

library(dplyr)
df1%>%group_by(grp)%>%summarise(Average=mean(response))
summarise() ungrouping output (override with .groups argument)
# A tibble: 4 x 2

## Output

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

 grp   Average
<chr> <dbl>
1  A   5.71
2  B   4.5
3  C     4
4  D     5

## Example 2

Following snippet creates a sample data frame −

Class<-sample(c("I","II","III"),20,replace=TRUE)
DP<-sample(1:10,20,replace=TRUE)
df2<-data.frame(Class,DP)
df2

The following dataframe is created −

 Class  DP
1   II  10
2    I  10
3    I   7
4   II   4
5   II   1
6   II   2
7  III   8
8    I   6
9   II   4
10   I   4
11 III   4
12   I   4
13   I  10
14 III   8
15 III   3
16  II   3
17 III   5
18   I   3
19 III   9
20   I   6

To find the mean of DP by factor levels in column Class, add the following code to the above snippet −

df2%>%group_by(Class)%>%summarise(Average=mean(DP))
summarise() ungrouping output (override with .groups argument)
# A tibble: 3 x 2

## Outpu

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

 Class  Average
<chr>  <dbl>
1   I   6.25
2  II      4
3 III   6.17