Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are all values in columns TRUE ignoring NA

I am working in R

Edit: hoping for a dplyr suggestion

sample data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df <- data.frame(col1 = c(TRUE, TRUE, NA, NA), 
                 col2 = c(FALSE,  TRUE, FALSE, NA), 
                 col3 = c(NA, TRUE, NA, TRUE))

print(df)

col1 col2 col3
TRUE FALSE NA
TRUE TRUE TRUE
NA FALSE NA
NA NA TRUE

Desired outcome

I would like a way of assessing whether col1 –> col3 are all TRUE, ignoring the NAs.

col1 col2 col3 assessment
TRUE FALSE NA FALSE
TRUE TRUE TRUE TRUE
NA FALSE NA FALSE
NA NA TRUE TRUE

>Solution :

With dplyr either using rowwise

df %>% 
  rowwise() %>% 
  mutate(assessment = all(across(everything()), na.rm=T)) %>% 
  ungroup()
# A tibble: 4 × 4
  col1  col2  col3  assessment
  <lgl> <lgl> <lgl> <lgl>     
1 TRUE  FALSE NA    FALSE     
2 TRUE  TRUE  TRUE  TRUE      
3 NA    FALSE NA    FALSE     
4 NA    NA    TRUE  TRUE 

or using pick, thanks @r2evans

mutate(df, assessment = apply(pick(col1:col3), 1, \(x) all(x, na.rm = TRUE)))
  col1  col2 col3 assessment
1 TRUE FALSE   NA      FALSE
2 TRUE  TRUE TRUE       TRUE
3   NA FALSE   NA      FALSE
4   NA    NA TRUE       TRUE

With base R, using all

cbind(df, assessment = apply(df, 1, \(x) all(x, na.rm=T)))
  col1  col2 col3 assessment
1 TRUE FALSE   NA      FALSE
2 TRUE  TRUE TRUE       TRUE
3   NA FALSE   NA      FALSE
4   NA    NA TRUE       TRUE
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading