Joseph Valde
Joseph Valde

Reputation: 31

filter column in data frame by two conditions in another column

Lets say we have something like this

df <- data.frame(species = c("Passer", "Turdus", "Turdus", "Gallus", "Anas", "Anas"),
                 season = c("breeding", "breeding", "non-breeding", "non-breeding", "breeding", "non-breeding"), 
                 value = seq(1, 12, 2), 
                 drop = c("no", "no", "dog", "dog", "cat", "cat"))
  species       season value drop
1  Passer     breeding     1   no
2  Turdus     breeding     3   no
3  Turdus non-breeding     5  dog
4  Gallus non-breeding     7  dog
5    Anas     breeding     9  cat
6    Anas non-breeding    11  cat

I would like to select the rows that have species with both values of breeding season, i.e. breeding and non-breeding.

The outcome should look like this

  species       season value drop
2  Turdus     breeding     3   no
3  Turdus non-breeding     5  dog
5    Anas     breeding     9  cat
6    Anas non-breeding    11  cat

I gave it a couple tries with filter() but I couldn’t make it work. I suspect making a loop could be the way to go? Thanks!

Upvotes: 0

Views: 28

Answers (1)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

If there are just the possibility of species appears two times, you can use this:

library(dplyr)

 df %>% 
   add_count(species) %>% 
   filter(n == 2) %>% 
   select(-n)

  species       season value drop
1  Turdus     breeding     3   no
2  Turdus non-breeding     5  dog
3    Anas     breeding     9  cat
4    Anas non-breeding    11  cat

Another way would be to do this

df %>% 
  group_by(species) %>% 
  filter(nlevels(factor(season)) == 2)

Upvotes: 1

Related Questions