Binning a numeric variable

前端 未结 3 1707
予麋鹿
予麋鹿 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: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
    

提交回复
热议问题