- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the column means by factor levels in R?
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
- Related Articles
- How to find the sum by distinct column for factor levels in an R data frame?
- How to find the number of levels in R for a factor column?
- How to extract the factor levels from factor column in an R data frame?
- How to find the median for factor levels in R?
- How to sum a variable by factor levels in R?
- How to find the column means of a column based on another column values that represent factor in an R data frame?
- How to create a new column for factor variable with changed factor levels by using mutate of dplyr package in R?
- How to rename the factor levels of a factor variable by using mutate of dplyr package in R?
- How to convert factor levels into character in R?
- How to find the cumulative sum for factor levels in an R data frame?
- How to collapse factor levels in an R data frame?
- How to print a factor vector without levels in R?
- How to find the maximum of factor levels in numerical column and return the output including other columns in the R data frame?
- How to find the column means for each matrix stored in an R list?
- How to find the sum by two factor columns in R?
