How do I subset a dataframe so that only rows that contain columns that have a value that shows up a certain amount of times in other rows are included.
For example, if I have a column labeled Food, how would I filter out all rows that have a food that shows up less than 5 times in the whole dataframe?
Here's a quick example:
dat <- data.frame(x=runif(50),y=sample(letters,50,replace = TRUE))
dat[dat$y %in% names(table(dat$y))[table(dat$y) > 2],]
That selects all rows that contain a letter that appears more than twice.
Here is another approach (probably cleaner) using plyr
.
ddply(dat, .(y), subset, length(x) > 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With