I am working on testing if the list of strings firearm_char is found as a substrings in a list of variables that are in a dataframe. rather than writing out all these queries, I would like to loop through. How do I approach having the loop treat the var list as column names instead of strings?
cod_acme_raw_vars <- c("ucod","acme1","acme2","acme3","acme4","acme5","acme6","acme7","acme8","acme9","acme10","acme11")
firearm_char <-c("W32","W33","W34","X72","X73","X74","X93","X94","X95","Y22","Y23","Y24","Y350","U014")
for (i in 1:length(cod_acme_raw_vars)) {
for (j in 1:length(firearm_char)) {
deaths16_21$all_firearm <- grepl( firearm_char[[j]] , cod_acme_raw_vars[[i]],)
}
}
I have tried several functions to treat the variable list as field names including . data, as.name.
Thank you! Robyn
grepl(paste(firearm_char, collapse = "|"), cod_acme_raw_vars)?