Binning a numeric variable

mcpeterson picture mcpeterson · Mar 24, 2010 · Viewed 25k times · Source

I have a vector X that contains positive numbers that I want to bin/discretize. For this vector, I want the numbers [0, 10) to show up just as they exist in the vector, but numbers [10,∞) to be 10+.

I'm using:

x <- c(0,1,3,4,2,4,2,5,43,432,34,2,34,2,342,3,4,2)
binned.x <- as.factor(ifelse(x > 10,"10+",x))

but this feels klugey to me. Does anyone know a better solution or a different approach?

Answer

unutbu picture unutbu · Mar 24, 2010

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+