0
votes

For example I have a vector: a = my_function(1000)!

head(a,15)
 [1]  0.4011032  0.4867019  0.9831197  1.1138037  3.2740297  3.7853916  4.6833426  6.9224802  7.5878639
[10]  8.0706788  8.4404792  9.4149317  9.4176043 10.2345215 11.1884374

I want to use cut function(or some alternative) to divide this vector into intervals. BUT I want, that maximum size of each interval will be, for example, 5.

EDITED:

breaks: breaks <- seq(from = 1, by = 4,length.out = 100)

So the first interval is: (1,5] . And first seven variables of a vector falls into this interval. But I want, that size of each intervall to be 5. It means that first 5 variables

[1]  0.4011032  0.4867019  0.9831197  1.1138037  3.2740297 

lies in first interval. And variables 3.7853916 4.6833426 lies in second interval(with length equall 5). How can I do that?

2
Define size. Length, range?ddunn801
@ddunn801 Edited. Take a look please once againDaniel Yefimov
@jogo But I think your code works only for n = 100.Daniel Yefimov
I think your question is still imprecise... Your a vector is your set of breaks, not an interval. First 10 are 1,6,13,19,25,31,37,43,49,55. How do those 10 fall in your interval (1,7]? If you want a "number of elements that fall inside your interval" to be 5, then you should use a modulus and a sort() or order() subsequently to the cut() or quantile() function. If you want "the interval to be of size 5", then your question is definitely not very clear... since 7-2 = 5 and 13-7 = 6, you're using some example numbers that really muddy up your intentions.quickreaction
@quickreaction I edited my question once more. Hope that everything is clear right now:)Daniel Yefimov

2 Answers

1
votes

Is this what you are looking for:

a <- rnorm(100)
a <- sort(a)
b <- matrix(data = a, nrow = 10, ncol = 10, dimnames = list(1:10))
1
votes

You can create a new variable which defines the group of belongness, just sorting the values and labeling them with a number (each one repeated 5 times).

cbind(sort(rnorm(100)), rep(1:20,each=5))

Note thate in rep, instead of 1:20 you should put n/5 being n the number of elements.

If you need to define the intervals, you need only to define theme using the median between the highest value of one set and the minimum one of the following.