Tomas H Tomas H - 13 days ago 6
R Question

How to keep only groups above certain number of rows?

library(tidyverse)

data<-diamonds%>%group_by(cut,color,clarity)%>%
summarize(aver=round(mean(price),0),count=n())%>%
filter(count>10)%>%
mutate(rank1=min_rank(desc(aver)),rank2=cume_dist(desc(aver)))


So when you run this script you will get output below. Now in cut and color column there are just 3 combinations " Fair D" which is also possible to see from rank1 column. Another group " Fair E" has 5 rows. I want to keep just rows for groups with more than 3 rows .

enter image description here

Answer

We can use filter with a logical condition (n() > 3) to keep only groups that have number of rows greater than a particular value

data %>% 
     filter(n()>3)
Comments