Groupby function in R using Dplyr – group_by

Groupby Function in R – group_by is used to group the dataframe in R.  Dplyr package in R is provided with group_by() function which groups the dataframe by multiple columns with mean, sum or any other functions.

We will be using iris data to depict the example of group_by() function

library(dplyr)
mydata2 <-iris 

# Groupby function for dataframe in R
summarise_at(group_by(mydata2,Species),vars(Sepal.Length),funs(mean(.,na.rm=TRUE)))

Mean of Sepal.Length is grouped by Species variable.

Group by function in R using dplyr 1

 

Groupby function in R with dplyr pipe operator %>%:

library(dplyr)
mydata2 <-iris # Group by function for dataframe in R using pipe operator mydata2 %>% group_by(Species) %>% summarise_at(vars(Sepal.Length),funs(sum(.,na.rm=TRUE)))

Sum of Sepal.Length is grouped by Species variable with the help of pipe operator (%>%) in dplyr package.

So the output will be

Group by function in R using dplyr 3

 

Groupby in R without dplyr using aggregate function:

In this example we will be using aggregate function in R to do group by operation as shown below

mydata2 <-iris 

# Group by in R using aggregate function

aggregate(mydata2$Sepal.Length, by=list(Species=mydata2$Species), FUN=sum)

Sum of Sepal.Length is grouped by Species variable with the help of aggregate function in R

Group by function in R using dplyr 2

Group by function in R using Dplyr                                                                                                                Group by function in R using Dplyr