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

Time:09-13

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

CodePudding user response:

I'm not sure if this is the best approach but you can do a nested ifelse (or its Tidyverse form if_else) and just add on to your conditions. Basically when the initial condition fails those items are passed to the secondary ifelse statement and so on:

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

newVar = dplyr::if_else(grepl("N", codes, fixed = T), 1,
                 if_else(grepl("H", codes, fixed = T), 2, 0))

CodePudding user response:

Hope this helps:

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)
  • Related