'Dividing on equal classes

I'm trying to create the percentage distribution of the number of the variable and draw a histogram of this distribution. I have to divide my variables on 9 equal ranges and sort them from lowest to biggest.

I was trying to use hist(salary) and sort it after all, but cannot do it right? Any tips for making the ranges equal and sorting?

r


Solution 1:[1]

You could cut your variable at sequence with range as endpoints and length 10 (which gives 9 breaks). Then count the length in groups using tapply and barplot the proportions.

dat <- transform(dat, g=cut(x, do.call(seq.int, c(as.list(range(x)), length.out=10)),
                            include.lowest=TRUE))
with(dat, tapply(x, g, length)) |> proportions() |> barplot()

enter image description here


Data:

n <- 1e3
set.seed(42)
dat <- data.frame(x=rnorm(n, 100, 25))

Solution 2:[2]

library(santoku)
x <- rnorm(100)
barplot(proportions(tab_evenly(x, 9)))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jay.sf
Solution 2 dash2