Split range of integers using integers only

Hello, I am trying to split a range of integers into quintiles:
[1] 10 10 10 10 11 11 11 12 13 13 15 15 17 18 19 20 22
[18] 25 25 25 26 28 31 31 32 32 32 36 36 38 39 43 44 47
[35] 48 55 55 60 70 71 121 134 169 181 268 318 344 354 431 436 479
[52] 489 520 700 925 1083 1106 1211 1324 1434

I want the breaks to be integers in my original range of values so the breaks might end up as 10-15; 17-31; 32- 55; 55-354; 431-1434.
I.e. there is no '16' in my values. Also there are a number of values of 55.

I am using quantile function at present and rounding e.g.: round(quantile(j$NUMBER_OF_PATIENTS, 0.2)) but I want to avoid the breaks starting and ending on the same integer e.g. 10-17; 17-31

Would anyone know of a different approach?
Many thanks

In this particular case, where your vector has a length that is a multiple of 5, you can find your desired values like this

NUMS <- c(10, 10, 10, 10, 11, 11, 11, 12, 13, 13, 15, 15, 17, 18, 19, 20, 22,
25, 25, 25, 26, 28, 31, 31, 32, 32, 32, 36, 36, 38, 39, 43, 44, 47,
48, 55, 55, 60, 70, 71, 121, 134, 169, 181, 268, 318, 344, 354, 431, 436, 479,
489, 520, 700, 925, 1083, 1106, 1211, 1324, 1434)
Uppers <- length(NUMS)/5 * 1:5
Lowers <- length(NUMS)/5 * 0:4 + 1
NUMS[sort(c(Lowers, Uppers))]
#>  [1]   10   15   17   31   32   55   55  354  431 1434

Created on 2023-11-29 with reprex v2.0.2
If the values will not always neatly group into quintiles, you will need a method to decide where to put the extra values.

NUMS <- c(10, 10, 10, 10, 11, 11, 11, 12, 13, 13, 15, 15, 17, 18, 19, 20, 22,
          25, 25, 25, 26, 28, 31, 31, 32, 32, 32, 36, 36, 38, 39, 43, 44, 47,
          48, 55, 55, 60, 70, 71, 121, 134, 169, 181, 268, 318, 344, 354, 431, 436, 479,
          489, 520, 700, 925, 1083, 1106, 1211, 1324, 1434)


(quant_breaks <- c(min(NUMS),quantile(x = NUMS,
         probs = (1:5)/5,
         type =1))) #Type 1 - Inverse of empirical distribution

#     20%  40%  60%  80% 100% 
# 10   15   31   55  354 1434 

# use the above info to make start and stop integer positions
# from which to construct the labels
(starts <- c(min(NUMS),head(tail(quant_breaks+1,-1),-1)))
(ends <- c(tail(quant_breaks,-1)))
(longvec <- c(rbind(starts,ends)))
(pairs <- split(longvec, 
                ceiling(seq_along(longvec)/2)))


(quant_labels <- unlist(lapply(pairs,
                       \(x){paste(x[1],x[2],
                                  sep=" - ",collapse="")})))
# 1            2            3            4            5 
# "10 - 15"    "16 - 31"    "32 - 55"   "56 - 354" "355 - 1434" 


(results <- cut(NUMS,
    breaks = quant_breaks,
    labels = quant_labels,
    include.lowest = TRUE))


table(results)
# 10 - 15    16 - 31    32 - 55   56 - 354 355 - 1434 
# 12         12         13         11         12

Thanks, this is what I needed for this. I appreciate the help

Thanks also @FJCC for your reply. This will come in useful

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.