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