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

前端 未结 2 599
孤独总比滥情好
孤独总比滥情好 2020-12-30 05:18

For the following data

ds <- read.table(header = TRUE, text =\"
id year attend
1 2007      1
1 2008      1
1 2009      1
1 2010      1
1 2011      1
8 200         


        
2条回答
  •  遥遥无期
    2020-12-30 05:44

    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
    

提交回复
热议问题