I have data like this, where some "name" occurs more than three times:
df <- data.frame(name = c("a", "a", "a", "b", "b", "c", "c", "c", "c"), x = 1:9)
name x
1 a 1
2 a 2
3 a 3
4 b 4
5 b 5
6 c 6
7 c 7
8 c 8
9 c 9
I wish to subset (filter) the data based on number of rows (observations) within each level of the name
variable. If a certain level of name
occurs more than say 3 times, I want to remove all rows belonging to that level. So in this example, we would drop observations where name == c
, since there are > 3
rows in that group:
name x
1 a 1
2 a 2
3 a 3
4 b 4
5 b 5
I wrote this code, but can't get it to work.
as.data.frame(table(unique(df)$name))
subset(df, name > 3)