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