1.10
The ifelse
function (part of base R) can be used for recoding data. The format is ifelse(test, return if TRUE, return if FALSE)
.
# if height is greater than 180
# then heightcat = "tall",
# otherwise heightcat = "short"
newdata <- mutate(starwars,
heightcat = ifelse(height > 180,
"tall",
"short"))
# set heights greater than 200 or less than 75 to missing
newdata <- mutate(starwars,
height = ifelse(height < 75 | height > 200,
NA, height))