Binning a numeric variable

前端 未结 3 1685
予麋鹿
予麋鹿 2020-12-15 10:45

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

相关标签:
3条回答
  • 2020-12-15 11:19
    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.

    0 讨论(0)
  • 2020-12-15 11:24

    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+
    
    0 讨论(0)
  • 2020-12-15 11:24

    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
    
    0 讨论(0)
提交回复
热议问题