I want to change variable "month" to a new variable "season". I tried two ways like below, but it seems like there are some errors in my code.
train_fd<-train_fd %>% mutate(season = ifelse(month <= 2 & month == 12,"winter",
ifelse(month <= 5 & month > 3,"spring",
ifelse(month <= 9 & month > 6,"summer","fall"))))
train_fd <- within(train_fd,
{
season = character(0)
season[month <= 2 & month == 12] = "winter"
season[month <= 5 & month >= 3] = "spring"
season[month <= 9 & month >= 6] = "summer"
season[month == 10 & month == 11] = "fall"
season = factor(season, level = c("winter","spring","summer","fall"))
})
I expect the output of level to be c("winter","spring","summer","fall")
, but the actual output is level
c("winter", "fall")