2
$\begingroup$

Don't know how else to phrase this question.

Say I want to split a number (514) into 20 approximately equal parts, and I want to know what each number would be. So for example, if I want to split 10 into 3 parts, I would want something like 3, 6, 10.

I know I can do something somewhat similar to what I want with cut

unique(cut(1:514,breaks=20))

which gives me the following output

[1] (0.487,26.6] (26.6,52.3]  (52.3,77.9]  (77.9,104]   (104,129]    (129,155]    (155,181]   
[8] (181,206]    (206,232]    (232,258]    (258,283]    (283,309]    (309,334]    (334,360]   
[15] (360,386]    (386,411]    (411,437]    (437,463]    (463,488]    (488,515]   

I can see those are the approximate breaks, but how can I just get the 20 integers where the breaks are?

$\endgroup$
0

2 Answers 2

4
$\begingroup$

This might work well enough in many cases:

round(seq(1, 514, by = 514/20))[-1]

You could also use floor or ceiling in place of round if that gets closer. Think of it as counting by 514/20ths.

$\endgroup$
1
$\begingroup$

The help file for cut suggests extracting the labels using regular expressions. In your example, it can be done like this.

f <- unique(cut(1:514,breaks=20))
labs <- levels(f)[f]
lower <- as.numeric( sub("\\((.+),.*", "\\1", labs))
upper <- as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs))

The break points for the cuts are the numbers in upper.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.