Binning a numeric variable
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.
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
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+