dplyr:根据聚合函数结果过滤行

前端之家收集整理的这篇文章主要介绍了dplyr:根据聚合函数结果过滤行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个表格列表(金额,年和月),我想过滤对应完整年份的行.即我想省略下面给出的示例数据帧的最后4行,这些行引用2015,其余的是60.是否可以使用单个dplyr命令执行此操作?

我试过这个:

df %>%
    group_by(year) %>%
    tally() %>%
    filter (n==12) %>%
    ungroup()

但我想ungroup会做出与我想要的不同的事情.是否可以使用单个dplyr命令执行此操作?

df <- structure(list(amount = c(16365,31850,32230,34177.75,27900,29650,28846,27300,37115.31,34130.38,39676.1,47244.44,3500,25425.48,22628.43,30822.86,30100,41567.13,25400,23125,40073.75,16505.82,17770,38406.03,1528.25,23475.77,29869.69,17020,19270,13085.47,10607.48,7800,15220,15260,17580,25094.66,3908.74,8150,25055.89,19690.65,12445.4,10347.39,7645.39,49300,8690,13660,16510,34457.08,522.68,10202,18900,25027.1,24956.42,23259,32743,37226,32697,32258,31336.67,36135.81,4389.26,12450,46220.43,36770.7),year = c("2010","2010","2011","2012","2013","2014","2015","2015"),month = c("01","02","03","04","05","06","07","08","09","10","11","12","01","04")),.Names = c("amount","year","month"),class = c("tbl_df","data.frame"),row.names = c(NA,-64L))
tally()相当于summary(n = n()).但是,在这种情况下,您希望保留数据框的原始行,但要进行过滤,以便删除属于不完整年份的行. @AndresT的答案可以正常工作,但你也可以更简洁地完成它,而无需创建一个列来计算每个组的行数的中间步骤:
df %>% group_by(year) %>% filter(n()==12)

猜你在找的设计模式相关文章