x <- 1:30
recode_into(
x > 15 ~ "a",
x > 10 & x <= 15 ~ "b",
default = "c"
)
set.seed(123)
d <- data.frame(
x = sample(1:5, 30, TRUE),
y = sample(letters[1:5], 30, TRUE),
stringsAsFactors = FALSE
)
# from different variables into new vector
recode_into(
d$x %in% 1:3 & d$y %in% c("a", "b") ~ 1,
d$x > 3 ~ 2,
default = 0
)
# no need to write name of data frame each time
recode_into(
x %in% 1:3 & y %in% c("a", "b") ~ 1,
x > 3 ~ 2,
data = d,
default = 0
)
Run the code above in your browser using DataLab