How to remove groups of observation with dplyr::filter()

别说谁变了你拦得住时间么 提交于 2019-11-30 06:47:39
Robert Krzyzanowski

Use filter in conjunction with base::ave

ds %>% dplyr::filter(ave(!is.na(attend), id, FUN = all))

To obtain

    id year attend
 1   1 2007      1
 2   1 2008      1
 3   1 2009      1
 4   1 2010      1
 5   1 2011      1
 6   9 2007      2
 7   9 2008      3
 8   9 2009      3
 9   9 2010      5
 10  9 2011      5

Or you could use:

ds %>%
group_by(id) %>% 
filter(attend=all(!is.na(attend)))
#Source: local data frame [10 x 3]
#Groups: id

#  id year attend
#1   1 2007      1
#2   1 2008      1
#3   1 2009      1
#4   1 2010      1
#5   1 2011      1
#6   9 2007      2
#7   9 2008      3
#8   9 2009      3
#9   9 2010      5
#10  9 2011      5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!