Dear all,

I'm looking for a function comparable to switch, to categorize a
continuous variable in a few levels. Off course that can be done with
a series of ifelse statements, but that looks rather clumsy. I looked
at switch, but couldn't figure out how to use it for this. I guess
that's not possible, as it only works with characters or integers, not
with intervals.

Basically, I'm looking for a clean way to do :
test <- runif(10,1,100)

testFunc <-function(x){
x <-  ifelse(test<10,"lowest",
        ifelse(10<=test & test <50,"low",
          ifelse(50<=test & test <90,"high",
            ifelse(90<=test & test<100,"highest",NA)
          )
        )
      )
return(as.factor(x))
}

testFunc(test)

Thank you in advance

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to