tags:

views:

250

answers:

3
+4  A: 

How about cut:

binned.x=cut(x, breaks=c(-1:9,Inf), labels=c(as.character(0:9),'10+'))

Which yields:

 [1] 0   1   3   4   2   4   2   5   10+ 10+ 10+ 2   10+ 2   10+ 3   4   2  
Levels: 0 1 2 3 4 5 6 7 8 9 10+
unutbu
That seems more complicated than the solution in the question.
Rob Hyndman
Minor improvements: Swap `1e6` with `Inf`. You don't need `include.lowest=TRUE`. (Compare answers by calling `table(binned.x)`.
Richie Cotton
@Rob: Yes, I can't say I disagree :)@Richie: Thanks! I'm still learning the language, so your "minor improvements" are a major help to me.
unutbu
+2  A: 

You question is inconsistent.
In description 10 belongs to "10+" group, but in code 10 is separated level. If 10 should be in the "10+" group then you code should be

as.factor(ifelse(x >= 10,"10+",x))

In this case you could truncate data to 10 (if you don't want a factor):

pmin(x, 10)
# [1]  0  1  3  4  2  4  2  5 10 10 10  2 10  2 10  3  4  2 10
Marek
+2  A: 
x[x>=10]<-"10+"

This will give you a vector of strings. You can use as.numeric(x) to convert back to numbers ("10+" become NA), or as.factor(x) to get your result above.

Note that this will modify the original vector itself, so you may want to copy to another vector and work on that.

James