Warm tip: This article is reproduced from serverfault.com, please click
cut r

Cut with one specific interval

发布于 2020-12-08 23:34:25

I have a dataset and I want to cut it into 4 parts using cut() function (or something similar).

set.seed(5)
cut(runif(100, 0, 100), 4)

gives levels: (1.35,25.8] (25.8,50.2] (50.2,74.6] (74.6,99]

The thing is, I want one interval to be (25.8,50] and another (50,74.6]. Actually, rest may vary a bit, but this 50 is important for me. How to achieve it?

Questioner
jedrekwol
Viewed
0
Suren 2020-12-10 12:30:52

If you are using cut, then the the first break has to be smaller than the min(data). Otherwise, there will be a NA in the intervals.

For example using quantiles,

cut(data, breaks= c(min(data) - diff(range(data)) / 1000,
                                             quantile(data)[2:5]))

Also, you already know the two middle intervals (25.8 - 50 and 50 - 74.6), its then just finding the min and max of the data

cut(data, breaks= c(min(data) - diff(range(data)) / 1000, 
                                        25.8, 50, 74.6, max(data)))