I need to code for a parallel test - ie if 1 out of 3 tests is positive; the individual (row) considered positive.
Found a couple of options through dplyr. But the catch is, not all tests are run on all individuals and in some cases no tests on an individual (row). Each code I use either completely ignores the row if there is an NA or considers the row negative if no tests were run falsely inflating the n of individuals tested. I've tried a series of rowSums, ifelse, case_when, and nothing calls everything perfectly. If there's anyways to manipulate this so it doesn't have to be categorized manually, that would be amazing.
PS sorry, I'm not really sure how to get the code looking correct but here's my attempt.
a b c
1 1 0
NA 1 0
NA NA NA
data <- data.frame(
a = c(1, NA, NA),
b = c(1, 1, NA),
c = c(0, 0, NA))
data <- data %>%
mutate(result=ifelse(a==1| b==1| c==1 ,1,0))
a b c result
1 1 0 1
NA 1 0 NA
NA NA NA NA
If I try with a na.rm = TRUE... and a sum of rows.
data %>%
rowwise() %>%
mutate(
sum = case_when( a== 1 | b==1|c==1 ~ sum(c_across(a:c), na.rm = TRUE)))
a b c result
1 1 0 2
NA 1 0 1
NA NA NA 0
The ideal result would be
a b c result
1 1 0 1 (or sum: 2)
NA 1 0 1
NA NA NA NA