dplyr - 使用带计数的过滤器 [英] dplyr - using filter with count
本文介绍了dplyr - 使用带计数的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用新的dplyr包,我想使用count()帮助过滤行。
我想输出的是 map%>%count(StudentID)= 3
的所有行。
Using the new dplyr package, 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的所有行,因为它们只有这些的两个实例,我想要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)
那么它只给我一个df 2列,但我想保留我的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.
推荐答案
我不认为 $ c>是你要找的。改用
n()
:
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
这篇关于dplyr - 使用带计数的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文