create size categories without nested ifelse in R
Asked Answered
K

1

1

Basically I got the problem solved, but I am trying to find a more elegant solution since the code gets a little bit hard to read. Here´s what I got:

mydf$size_class = ifelse(mydf$absolute_number <= 5,"1-5",ifelse(mydf$absolute_number > 6 &    
mydf$absolute_number <= 10,"6-10","x"))

Maybe I need rather some formatting help / hints, convention than a new code :) – those are also very welcome ;)

Korea answered 20/7, 2010 at 8:52 Comment(0)
T
4

Try the cut function:

R> x <- 1:10
R> cut(x, breaks = c(0, 5, 10), labels=c("1-5", "6-10"))
 [1] 1-5  1-5  1-5  1-5  1-5  6-10 6-10 6-10 6-10 6-10
Levels: 1-5 6-10
Tirza answered 20/7, 2010 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.