0

I have data points ranging from 432 - 1789. I want to divide this data into 3 equal parts. For example, I should first arrange this in ascending order and then divide into 3 equal parts. and categorize into 'Low', 'Medium' and 'High'

How can I do this in R?

I used this, but I don't think it divided the data correctly,

x$level <- cut(x$newlevels, 3, include.lowest=TRUE, labels=c("Low", "Med", "High"))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 2
    Why do you think that didn't work? It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. What if the total number of elements are not divisible by 3? What if they were all the same value? What if there were only 2 unique values? What should happen? – MrFlick Jan 10 '18 at 18:46
  • 1
    Works with fake data `set.seed(7866);x <- data.frame(newlevels = sample(432:1789, 1e4, TRUE))`. Then your command, then `table(x$level)` gives comparable figures. (I didn't arrange in ascending order.) – Rui Barradas Jan 10 '18 at 18:50
  • what you have works. But, @MrFlick raised good points. Can you elaborate your need ? `df<-data.frame( x = seq(432:1789)) df$levels <- cut(df$x, 3, include.lowest=TRUE, labels=c("Low", "Med", "High")) head(df)` – user5249203 Jan 10 '18 at 18:55
  • if the points are not normally distributed, and you want each group to contain the same number of points then you may need the quantile function: `cut(x, quantile(x, c(0, 0.33, 0.66, 1)), include.lowest=TRUE)` – Dave2e Jan 10 '18 at 19:51

0 Answers0