Creating a complex filter for a dataset in R

0

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!

Like this?

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 == 0 & mother == 0 & children == 0 & cousins == 0))
  father mother children cousins
1      1      1        0       0
2      1      1        0       5

Thanks! Are these scripts being run in tidyverse? If so, I am receiving the following error while running this package:

── Conflicts ────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
:heavy_multiplication_x: dplyr::filter() masks stats::filter()
:heavy_multiplication_x: dplyr::ident() masks dbplyr::ident()
:heavy_multiplication_x: dplyr::lag() masks stats::lag()
:heavy_multiplication_x: dplyr::sql() masks dbplyr::sql()

Any suggestions on how I can resolve this error in order to run the scripts you have shared above?

Those are not error messages, just warnings to let you know that some functions being loaded have the same name in another already loaded package, so you are aware of it.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.