I'm trying to figure out a way for me to use grepl()
of only one partial pattern over multiple columns with mutate()
. I want to have a new column that will be TRUE or FALSE if ANY of a set of columns contains a certain string.
df <- structure(list(ID = c("A1.1234567_10", "A1.1234567_20"),
var1 = c("NORMAL", "NORMAL"),
var2 = c("NORMAL", "NORMAL"),
var3 = c("NORMAL", "NORMAL"),
var4 = c("NORMAL", "NORMAL"),
var5 = c("NORMAL", "NORMAL"),
var6 = c("NORMAL", "NORMAL"),
var7 = c("NORMAL", "ABNORMAL"),
var8 = c("NORMAL", "NORMAL")),
.Names = c("ID", "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8"),
class = "data.frame", row.names = c(NA, -2L))
ID var1 var2 var3 var4 var5 var6 var7 var8
1 A1.1234567_10 NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL
2 A1.1234567_20 NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL ABNORMAL NORMAL
I tried
df$abnormal %>% mutate( abnormal = ifelse(grepl("abnormal",df[,119:131]) , TRUE, FALSE)))
and about 100 other things. I want the final format to be
ID var1 var2 var3 var4 var5 var6 var7 var8 abnormal
1 A1.1234567_10 NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL FALSE
2 A1.1234567_20 NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL ABNORMAL NORMAL TRUE
Whenever I try I get false every time