Reputation: 1441
I have a data frame where I want to filter out the Month values if the count of its associated Index is < 2.
ID = c(rep("A", 5), rep("B", 5))
Month = c(
1,
1,
2,
2,
3,
1,
2,
2,
3,
3
)
Index = c("X1", "X1", "X2", "X2", "X3", "X1", "X2", "X2", "X2", "X3")
df = data.frame(ID, Month, Index, stringsAsFactors = FALSE)
df$Month <- as.factor(df$Month)
df
Here, X3 and X1 only occur once for A and B respectively so those rows would be deleted.
But if I try to filter my data using %in%
for X3 and X1 they will also get removed from the other ID values.
The deletion should only be group specific.
Upvotes: 0
Views: 260
Reputation: 887118
We can group by the columns and filter
library(dplyr)
df %>%
group_by(ID, Month, Index) %>%
filter(n() >1)
Upvotes: 1