I\'m facing nested ifelse()
structures:
df1$var <- ifelse(x < a, u, ifelse(x < b, v, ifelse(x < c, w, ...)))
whereby t
In base R
, if there are multiple elements to be replaced, create a key/value dataset and do a merge
keyval <- data.frame(x = c(1, 2, 3, 4), y = c("s", "t", "u", "v"), stringsAsFactors = FALSE)
new <- merge(df1, keyval, by = 'x', all.x = TRUE)[['y']]
new[is.na(new)] <- "w"
df1$x <- new
set.seed(24)
df1 <- data.frame(x = rbinom(100, 5, .5))