0

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

2
  • Could you give more context around the data frame 'deaths16_21' and what is cod_acme_raw_vars for? What is your expected outcome for a sample? Commented Jan 25, 2023 at 22:46
  • Maybe you want grepl(paste(firearm_char, collapse = "|"), cod_acme_raw_vars) ? Commented Jan 26, 2023 at 8:55

2 Answers 2

1

I am also unsure of what you're trying to accomplish exactly, but if you are trying to create a subset of the data frame by matching the values in your list, then you can do something like this with the filter function using filter_all from library(dplyr).

A <- data.frame(ColumnOne = c("ucod","maybe","there","is","one"), 
                ColumnTwo = c("there", "W32","W33","W34", "are"))

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")

AppendedList = append(cod_acme_raw_vars, firearm_char)

B <- A %>%
  filter_all(any_vars(. %in% AppendedList))

Since you want to check for all of the items in the list, you can append the lists together so that you only search from one list with all of the values. Data frame B becomes the subset of all the rows which match with the values in your lists.

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

you can try to use the match as below

 match(firearm_char,cod_acme_raw_vars)

this will help check the list of strings from firearm_char with the list of strings in cod_acme_raw_vars, if a match is found then a index position number will return

 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA 12

based on that position we can identify that string from firearm_char and use it further.

I am not clear with what you would like to accomplish

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.