This is my sample data:
id <- c("1a","2c","3d","4f","5g","6e","7f","8q","9r","10v","11x","12l")
dg1 <- c(111234,902754,111235,111236,113456,123754,288563,396186,987654,987865,288563,396186)
dg2 <-c("",111235,111236,113456,"","","","","","",902754,902754)
df<-cbind(id,dg1,dg2)
I'd like to create a new column that indicates whether a string (111 or 113) is present across multiple columns such that my final df looks like this:
dt1 <- c(1,1,1,1,1,0,0,0,0,0,0,0)
df <- cbind(df,dt1)
I've tried to do this:
df %>%
filter(any_vars(grepl('^(113|111)')))
and
df %>%
select(contains("113","111"))
with the intention of tagging the filtered rows with mutate then doing a left join into the original data frame.
I've tried to Frankenstein together some code from similar problems to no avail here and here. I'm trying to avoid this solution because my actual data has over a hundred columns to sort through with dozens of strings to search through (trying to avoid typing out and potentially missing a combination).
Can anyone help?