Create a column based on single match, two matches, etc...

Hello everybody!
I'm trying to create a column and/or several columns that would indicate:
a)at least one true present in a row ( within specific columns)
b)one true is present in a row( within specific columns)
b) two TRUE present in a row( within specific columns)
c) more than two "TRUE" present in a row( within specific columns)

I was only able to solve the simplest case with str_detect. Your help would be greatly appreciated.

library(tidyverse)
df <- data.frame(flag1 = c(FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE), 
                 flag2 = c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
                 flag3 = c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE))

df %>%
  mutate(any_true = if_any(flag1:flag3, ~ str_detect(., pattern = "TRUE")))
#>    flag1 flag2 flag3 any_true
#> 1  FALSE FALSE  TRUE     TRUE
#> 2   TRUE FALSE  TRUE     TRUE
#> 3  FALSE FALSE FALSE    FALSE
#> 4   TRUE  TRUE  TRUE     TRUE
#> 5   TRUE  TRUE  TRUE     TRUE
#> 6  FALSE  TRUE FALSE     TRUE
#> 7  FALSE FALSE FALSE    FALSE
#> 8   TRUE  TRUE  TRUE     TRUE
#> 9   TRUE  TRUE  TRUE     TRUE
#> 10  TRUE  TRUE  TRUE     TRUE
#> 11 FALSE FALSE FALSE    FALSE
#> 12 FALSE FALSE FALSE    FALSE
#> 13 FALSE FALSE FALSE    FALSE

Since TRUE = 1 and FALSE = 0, I would use sum() instead of str_detect().

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
df <- data.frame(flag1 = c(FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE), 
                 flag2 = c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
                 flag3 = c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE))
dfNew <- df |> rowwise() |> 
  mutate(AnyTrue = sum(c_across(flag1:flag3)) > 0,
         OneTrue = sum(c_across(flag1:flag3)) == 1,
         TwoTrue = sum(c_across(flag1:flag3)) == 2,
         MoreThanTwo = sum(c_across(flag1:flag3)) > 2)
dfNew
#> # A tibble: 13 x 7
#> # Rowwise: 
#>    flag1 flag2 flag3 AnyTrue OneTrue TwoTrue MoreThanTwo
#>    <lgl> <lgl> <lgl> <lgl>   <lgl>   <lgl>   <lgl>      
#>  1 FALSE FALSE TRUE  TRUE    TRUE    FALSE   FALSE      
#>  2 TRUE  FALSE TRUE  TRUE    FALSE   TRUE    FALSE      
#>  3 FALSE FALSE FALSE FALSE   FALSE   FALSE   FALSE      
#>  4 TRUE  TRUE  TRUE  TRUE    FALSE   FALSE   TRUE       
#>  5 TRUE  TRUE  TRUE  TRUE    FALSE   FALSE   TRUE       
#>  6 FALSE TRUE  FALSE TRUE    TRUE    FALSE   FALSE      
#>  7 FALSE FALSE FALSE FALSE   FALSE   FALSE   FALSE      
#>  8 TRUE  TRUE  TRUE  TRUE    FALSE   FALSE   TRUE       
#>  9 TRUE  TRUE  TRUE  TRUE    FALSE   FALSE   TRUE       
#> 10 TRUE  TRUE  TRUE  TRUE    FALSE   FALSE   TRUE       
#> 11 FALSE FALSE FALSE FALSE   FALSE   FALSE   FALSE      
#> 12 FALSE FALSE FALSE FALSE   FALSE   FALSE   FALSE      
#> 13 FALSE FALSE FALSE FALSE   FALSE   FALSE   FALSE

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

1 Like

This was smart. Thank you, FJCC!

This topic was automatically closed 7 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.