Home > Enterprise >  Creating a complex filter for a dataset in R
Creating a complex filter for a dataset in R

Time:06-05

I have a sample dataset which is as follows:

father<- c(1, 1, 1, 0, 0)
mother<- c(1, 1, 1, 0, 0) 
children <- c(0, 0, 2, 5, 2) 
cousins   <- c(0, 5, 1, 1, 4) 


dataset <- data.frame(father, mother, children, cousins)  
dataset


father  mother  children cousins
1      1       0      0
1      1       0       5
1      1        2       1
0     0        5       1
0     0        2       4

I would like to apply a condition-based filter so I can get all fathers marked with a '1' and mothers would be 0 OR children would be 0 OR cousins would be 0. In addition, I would also like this filter to select for all fathers with a 0 AND mothers with a 0 AND children with a 0 AND cousins with a 0. Any ideas on how I can go about creating such a filter for my dataset.

Thank you!

CodePudding user response:

Is this what you're looking for:

library(dplyr)
father<- c(1, 1, 1, 0, 0)
mother<- c(1, 1, 1, 0, 0) 
children <- c(0, 0, 2, 5, 2) 
cousins   <- c(0, 5, 1, 1, 4) 


dataset <- data.frame(father, mother, children, cousins) 

dataset %>% 
  filter(father == 1 & (mother == 0 | children == 0 | cousins == 0))
#>   father mother children cousins
#> 1      1      1        0       0
#> 2      1      1        0       5

dataset %>% 
  filter(father == 0 & mother == 0 & children == 0 & cousins == 0)
#> [1] father   mother   children cousins 
#> <0 rows> (or 0-length row.names)

Created on 2022-06-04 by the reprex package (v2.0.1)

CodePudding user response:

A variation on @Dave's answer using if_any and if_all:

library(tidyverse)

father<- c(1, 1, 1, 0, 0)
mother<- c(1, 1, 1, 0, 0) 
children <- c(0, 0, 2, 5, 2) 
cousins   <- c(0, 5, 1, 1, 4) 


dataset <- data.frame(father, mother, children, cousins) 

dataset |> 
  filter(father == 1, if_any(-father, ~ . == 0))
#>   father mother children cousins
#> 1      1      1        0       0
#> 2      1      1        0       5

dataset |> 
  filter(if_all(, ~ . == 0))
#> [1] father   mother   children cousins 
#> <0 rows> (or 0-length row.names)

Created on 2022-06-04 by the reprex package (v2.0.1)

  • Related