-2

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")
Vega
  • 27,856
  • 27
  • 95
  • 103
  • 3
    Please read [how to create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (and ask a question that is likely to get answered). – heds1 Jul 15 '19 at 03:31

2 Answers2

0

You are better off using %in% operator -

ifelse(month %in% c(1,2,12), "winter",
       ifelse(month %in% c(3,4,5), "spring",
              ifelse(month %in% c(6,7,8,9), "summer", "fall")))

Would also suggest you to look at ?case_when() from dplyr.

Shree
  • 10,835
  • 1
  • 14
  • 36
0

Frankly, I would save myself a ton of trouble doing an explicit dictionary:

m2s <- c( rep("winter", 2), 
          rep("spring", 3), 
          rep("summer", 4), 
          rep("fall", 2), "winter)

And now you can simply

train_fd$season <- m2s[ train_fd$month ]

Note the problems in your first approach:

  • month <= 2 & month == 12 always returns FALSE, because you used & instead of |; therefore January, February and December all become "fall";
  • month <= 5 & month > 3 what if month == 3? Then March is fall...
  • month <= 9 & month > 6 again, June becomes fall.

Your second approach replicates some of these errors (such as & for winter) and add new ones (such as condition month == 10 & month == 11, which always returns FALSE). I have not the slightest idea, however, how you can get winter as an output level in any of the approaches: this is not possible.

Bottom line is: multilayered conditionals are prone to typos.

January
  • 16,320
  • 6
  • 52
  • 74