I've searched extensively on stack overflow without finding an answer to the following question: Im looking for a function that lets me test if some columns contain any of the specified strings.
# I'm looking for heart attacks
infarction <- c("b", "c")
# I'm also looking for strokes
stroke <- c("h", "i")
#sample data set
set.seed(1234)
dat <- data.frame(A = sample(letters[1:9],10,TRUE),
B = sample(letters[1:9],10,TRUE),
C = sample(letters[1:9],10,TRUE),
D = sample(letters[1:9],10,TRUE),
DATE = sample.int(10, size = 10, replace = FALSE))
# I've tried many things. Among them:
# first one using the dplyr package.
infarction = ifelse( (infarction %in% dat[,c("A", "B", "C", "D")]), DATE, NA))
#excluded a few rows from the mutate...
#I've also tried
grep(paste(infarction,collapse="|"), dat[,1:4], value=TRUE), DATE, NA))
To sum up. I can get it to work if i only check in one column or only look after one of the strings. But I need to check if any of the strings is contained in any of the given columns, and in that case return the date value as a new variable.
Any help is greatly appreciated.