如何根据类别计数过滤数据框 [英] How To Filter a Dataframe based on Category Counts
本文介绍了如何根据类别计数过滤数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何子集数据框,以便仅包含包含其值在其他行中显示一定时间的列的行.
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.
例如,如果我有一个标记为食物"的列,我该如何过滤掉所有食物在整个数据框中显示少于5次的所有行?
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.
这篇关于如何根据类别计数过滤数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文