Ungroup after grouping by just one variable in dplyr

Viewed 21796

I have a lot of units that are measured repeated times.

>df
Item value  year
1     20     1990
1     20     1991
2     30     1990
2     15     1990
2     5      1991
3     10     1991
4     15     1990
5     10     1991
5      5     1991

I am trying to use dplyr to remove values that have a low number of observations. On this toy data, lets say that I want to remove data which has fewer than 2 counts.

>df <- df %>% 
  group_by(Item) %>% 
  tally() %>% 
  filter(n>1)

Item  n
1     2
2     3
5     2

The problem is that I would like to expand this back to what it was, but with this filter. I attempted using the ungroup command, but that seems to only have an effect when grouping by two variables. How can I filter by item counts then get my original variables back i.e value and year. It should look like this:

>df
Item value  year
1     20     1990
1     20     1991
2     30     1990
2     15     1990
2     5      1991
5     10     1991
5      5     1991
1 Answers
Related