Search code examples
rregexstringfunctionvector

R – How to create a function that matches a string and then creates new variable from it?


I have a variable called "codes".

codes <- c("N011", "H002", NA, NA, "D001", NA, "N011")

I would like to create a function where the arguments are any part of the character strings in codes, for example: "N01" or "H", so the function identifies those that match and assigns a number to each on a separate variable. In this case, with arguments "N01" and "H", I would like the new variable to be something like this:

new_variable
1
2
0
0
0
0
1

I created a function capable of doing this with a single argument, but I haven't been able to create one with n number of arguments:

fun <- function(x, codes) {
  mutate(data, new_variable = ifelse(grepl(x, codes, fixed = TRUE), 1, 0))
}

fun("N001", codes)
new_variable
1
0
0
0
0
0
1

Solution

  • library(stringr)
    
    codes <- c("N011", "H002", NA, NA, "D001", NA, "N012")
    
    to_find <- c("N01", "H")
    
    find <- function(to_find, x) {
      res <- vector("integer", length(x))
      for (i in seq_along(to_find)) {
        res[str_detect(x, to_find[i])] <- i
      }
      res
    }
    
    find(to_find, codes)