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

前端 未结 2 583
孤独总比滥情好
孤独总比滥情好 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:30

    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
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题