Remove groups with less than three unique observations

With data.table you could do:

library(data.table)
DT[, if(uniqueN(Day) >= 3) .SD, by = Group]

which gives:

   Group Day
1:     1   1
2:     1   3
3:     1   5
4:     1   5
5:     3   1
6:     3   2
7:     3   3

Or with dplyr:

library(dplyr)
DT %>% 
  group_by(Group) %>% 
  filter(n_distinct(Day) >= 3)

which gives the same result.


One idea using dplyr

 library(dplyr)
 df %>% 
   group_by(Group) %>% 
   filter(length(unique(Day)) >= 3)

#Source: local data frame [7 x 2]
#Groups: Group [2]

#  Group   Day
#  (int) (int)
#1     1     1
#2     1     3
#3     1     5
#4     1     5
#5     3     1
#6     3     2
#7     3     3

We can use base R

i1 <-  rowSums(table(df1)!=0)>=3
subset(df1, Group %in% names(i1)[i1])
#    Group Day
#1      1   1
#2      1   3
#3      1   5
#4      1   5
#9      3   1
#10     3   2
#11     3   3

Or a one-liner base R would be

df1[with(df1, as.logical(ave(Day, Group, FUN = function(x) length(unique(x)) >=3))),]