简体   繁体   中英

cut continuous variables to categorical variables in r (with separate values as groups)

I have a continuous variable ranging from 0 to 1000+. However, I want to convert them into groups with the lowest value 0 as a single group.

x <- cut(x, breaks = c(0,50,100,200,500,1000)),
                   labels = c("0","1-50","51-100","101-199","200-499",
                              "500-999",">1000")

Using cut function has not been able to achieve this purpose. Is there anything that I'm missing?

You've got some issues in your call to cut

  1. You've got a closing bracket after your breaks that should be there, prematurely ending the call.
  2. You've too many labels (7) for the breaks (5).

This should work:

x <- cut(x, breaks = c(0,50,100,200,500,1000,max(x)),labels = c("0-50","51-100","101-199","200-499","500-999", "1000+"), include.lowest = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM