Besides the suggestions in the comments, you can also use match as follows.
Create sample data:
set.seed(1) vals_in <- c("x", "y", "z") # unique values in your input vector vec_in <- sample(vals_in, 10, replace = TRUE) # sample from vals_in to create input vals_out <- c("x1", "x2", "x3") # values to replace
Now, to replace the nested ifelse , you could do:
vec_out <- vals_out[match(vec_in, vals_in)]
Result
vec_out # [1] "x1" "x2" "x2" "x3" "x1" "x3" "x3" "x2" "x2" "x1"
A small comparison of the two approaches:
set.seed(1) vals_in <- letters vec_in <- sample(vals_in, 1e7, replace = TRUE) vals_out <- LETTERS system.time(vals_out[match(vec_in, vals_in)]) User System verstrichen 0.378 0.020 0.398 system.time(unname(setNames(vals_out, vals_in)[vec_in])) User System verstrichen 1.020 0.062 1.084
source share