Search code examples
rsubset

R subseting ID that have only one value in a column


I have a bird sighting dataset:

df
    # Bird.ID Location Date
    # 1 Plot1 22/02/2022
    # 2 Plot5 22/02/2022
    # 3 Plot1 22/02/2022
    # 1 Plot1 24/02/2022
    # 1 Plot1 26/02/2022
    # 1 Plot1 22/03/2022
    # 2 Plot5 22/03/2022
    # 2 Plot5 14/04/2022
    # 3 Plot2 14/04/2022
    # 3 Plot3 22/06/2022

    

I want to subset and keep only the individual that stayed in the same location through time. In this example I want to get rid of Individual "3" That was seen in 3 different location (plot1, 2 & 3) and keep only individual 1 (who stayed in plot1) & individual 2 (stayed in plot5)

The wanted output would be something like this:

output
    # Bird.ID Location Date
    # 1 Plot1 22/02/2022
    # 2 Plot5 22/02/2022
    # 1 Plot1 24/02/2022
    # 1 Plot1 26/02/2022
    # 1 Plot1 22/03/2022
    # 2 Plot5 22/03/2022
    # 2 Plot5 14/04/2022

Solution

  • A possible solution:

    library(dplyr)
    
    df %>% 
      group_by(Bird.ID) %>% 
      filter(n_distinct(Location) == 1) %>% 
      ungroup
    
    #> # A tibble: 7 × 3
    #>   Bird.ID Location Date      
    #>     <int> <chr>    <chr>     
    #> 1       1 Plot1    22/02/2022
    #> 2       2 Plot5    22/02/2022
    #> 3       1 Plot1    24/02/2022
    #> 4       1 Plot1    26/02/2022
    #> 5       1 Plot1    22/03/2022
    #> 6       2 Plot5    22/03/2022
    #> 7       2 Plot5    14/04/2022