Home > other >  Selectively Filtering Across Multiple Variables in R
Selectively Filtering Across Multiple Variables in R

Time:10-26

I'm using Tidyverse

animals <- c("Bat", "Monkey", "Tiger", "Frog", "Bald Eagle")
Type <- c("mammal","mammal","mammal", "Amphibian", "Bird")
Quantity <- c(8, 5, 1, 5, 1)
Bananas <-  c(0,1,0,0,0)
Apples <- c(0,1,0,0,0)
Crickets <- c(1,0,0,1,0)
DF <- data.frame(animals, Type, Quantity,Bananas, Apples, Crickets)

#DF looks like this 
     animals      Type Quantity Bananas Apples Crickets
1       Bat     mammal        8       1      1        0
2     Monkey    mammal        5       1      1        0
3      Tiger    mammal        1       0      0        0
4       Frog    Amphibian     5       0      0        1
5 Bald Eagle    Bird          1       0      0        0

I'm looking for a simple way to filter for a result that shows:

  1. only rows that are mammals and
  2. only rows where Bananas, OR Apples, OR Crickets are equal to 1
#desired results
  animals   Type Quantity Bananas Apples Crickets
1    Bat  mammal        8       0      0        1
2  Monkey mammal        5       1      1        0

I've been trying to use filter_all(any_vars) and do conditions, but in the instances where it kind of worked, I'm still having Tiger show up on the final list, due to it having "1" in the quantity variable.

I need a clean way to do this filtering, I don't want to lose any columns, in the real dataset there are 50 columns of other info that need to be kept.

Thank you!

CodePudding user response:

Assuming inclusive OR for your constraint 2, then

DF %>%
  filter(
    Type == "mammal",
    rowSums(select(., Bananas:Crickets) == 1) > 0
  )
#   animals   Type Quantity Bananas Apples Crickets
# 1     Bat mammal        8       0      0        1
# 2  Monkey mammal        5       1      1        0

CodePudding user response:

filtering twice should work, using the | operator for the second call.

DF %>%
    filter(Type == "mammal", Bananas == 1 | Apples == 1 | Crickets == 1)

More explicitly, in two separate filter calls:

DF %>%
    filter(Type == "mammal") %>%
    filter(Bananas == 1 | Apples == 1 | Crickets == 1)
  • Related