Groupby Count in R

Groupby count in R can be accomplished by aggregate() or group_by() function. Let’s see how to

  • Groupby count of single column in R
  • Groupby count of multiple columns in R

First let’s create a dataframe

df1= data.frame(Name=c('James','Paul','Richards','Marico','Samantha','Ravi','Raghu','Richards','George','Ema','Samantha','Catherine'),
    State=c('Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'),
    Sales=c(14,24,31,12,13,7,9,31,18,16,18,14))
df1

df1 will be

groupby count in R 1

 

Groupby count of single column in R

Method 1:

Aggregate function along with parameter by – by which it is to be grouped and function length, is mentioned as shown below

# Groupby count of single column

aggregate(df1$Sales, by=list(df1$State), FUN=length)

so the grouped dataframe will be

groupby count in R 2a

Method 2: groupby using dplyr

library(dplyr)
df1 %>% group_by(State) %>% summarise(count_sales = n())

so the grouped dataframe will be

groupby count in R 2b

 

 

Groupby count of multiple column in R

Method 1:

Aggregate function which is grouped by state and name, along with function length is mentioned as shown below

# Groupby count of multiple columns

aggregate(df1$Sales, by=list(df1$State,df1$Name), FUN=length)

so the grouped dataframe will be

groupby count in R 3a

Method 2: groupby using dplyr

library(dplyr)
df1 %>% group_by(State,Name) %>% summarise(count_sales = n())

so the grouped dataframe will be

groupby count in R 3b