Home > Net >  Using a list of variables in a loop
Using a list of variables in a loop

Time:01-26

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

CodePudding user response:

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

CodePudding user response:

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!

  • Related