0

I have a data frame that has numbers ranging between -1 and +1. I'd like to replace those numbers with a Low, Medium, or High based on the range they fall into.

Is there a way to create a function using ifelse conditions and then apply labels to the elements depending on where they fall in the range?

Cheers,

Cybernetic
  • 12,628
  • 16
  • 93
  • 132

1 Answers1

1
ifelse(variable < 0, "low", ifelse(variable < 1, "medium", "high"))

Timings for various methods:

variable <- data.frame(matrix(rnorm(1e5*6), ncol = 6))

tst2 <- system.time(cut(as.matrix(variable), breaks = c(-Inf, -1, 1, Inf), labels = c("low","medium","high")))
tst1 <- system.time(ifelse(variable < 0, "low", ifelse(variable < 1, "medium", "high")))
tst3 <- system.time(lapply(variable, cut, breaks=c(-Inf, -1, 1, Inf), labels=c("low", "medium", "high")))


> data.frame(tst1["elapsed"],tst2["elapsed"],tst3["elapsed"])
        tst1..elapsed.. tst2..elapsed.. tst3..elapsed..
elapsed            0.46            0.15            0.17
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Awesome thanks. For anyone else, I simply used the above in a function and ran the function against all elements in the data frmae using apply. – Cybernetic Mar 04 '14 at 18:42
  • @Cybernetic, that sounds like an inefficient approach. `ifelse` is already the least efficient answer in the linked duplicate question. – A5C1D2H2I1M1N2O1R2T1 Mar 04 '14 at 19:07
  • Now, compound that with the fact that the OP is unnecessarily using `apply` and I hope that my statement that their approach is inefficient is validated. If you're working with a small dataset, no big deal. If it's part of a larger set of code that's being processed, the time differences could quickly add up, and I didn't bother check to see how they scale. Regarding `ifelse`, see also this: http://stackoverflow.com/q/16275149/1270695 – A5C1D2H2I1M1N2O1R2T1 Mar 04 '14 at 19:51