使用带计数的过滤器 [英] Using filter with count
本文介绍了使用带计数的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 count()
帮助程序过滤行.我想要的输出是 map %>% count(StudentID) = 3
的所有行.
I'm trying to filter row using the count()
helper.
What I would like as output are all the rows where the map %>% count(StudentID) = 3
.
例如在下面的 df 中,它应该删除所有包含 StudentID 10016 和 10020 的行,因为它们只有 2 个实例,而我想要 3 个.
For instance in the df below, it should take out all the rows with StudentID 10016 and 10020 as they are only 2 instances of these and I want 3.
StudentID StudentGender Grade TermName ScaleName TestRITScore
100 M 9 Fall 2010 Language Usage 217
100 M 10 2011-2012 Language Usage 220
100 M 9 Fall 2010 Reading 210
10016 M 6 Fall 2010 Language Usage 217
10016 M 6 Fall 2010 Mathematics 210
10020 F 7 Fall 2010 Language Usage 210
10020 F 7 Fall 2010 Mathematics 213
10022 F 8 Fall 2010 Language Usage 232
10022 F 9 2011-2012 Language Usage 240
10022 F 8 Fall 2010 Mathematics 242
如果我这样做:
count(df, StudentID)
然后它只给了我一个有 2 列的 df,但我想保留我的 df 的所有列.这就是为什么我认为我应该使用过滤器.
then it only gives me a df with 2 columns, but I want to keep all the columns of my df. thats's why I think I should use filter.
推荐答案
我认为 count
不是您要找的.试试 n()
代替:
I don't think count
is what you looking for. Try n()
instead:
df %>%
group_by(StudentID) %>%
filter(n() == 3)
# Source: local data frame [6 x 6]
# Groups: StudentID
#
# StudentID StudentGender Grade TermName ScaleName TestRITScore
# 1 100 M 9 Fall 2010 Language Usage 217
# 2 100 M 10 2011-2012 Language Usage 220
# 3 100 M 9 Fall 2010 Reading 210
# 4 10022 F 8 Fall 2010 Language Usage 232
# 5 10022 F 9 2011-2012 Language Usage 240
# 6 10022 F 8 Fall 2010 Mathematics 242
这篇关于使用带计数的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文