简体   繁体   English

如何将数值向量切成最大长度为第i个间隔等于n的间隔

[英]How to cut numeric vector into intervals with maximum length of i-th interval equall n

For example I have a vector: a = my_function(1000) ! 例如我有一个向量: 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. 我想使用cut函数(或一些替代方法)将此向量划分为间隔。 BUT I want, that maximum size of each interval will be, for example, 5. 但我想要每个间隔的最大大小为例如5。

EDITED: 编辑:

breaks: breaks <- seq(from = 1, by = 4,length.out = 100) 休息时间: 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,5]和前七个变量。 a载体落入此区间,但我想,每个INTERVALL的是尺寸为5。这意味着前5个变量。

[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). 变量3.7853916 4.6833426位于第二个间隔(长度等于5)。 How can I do that? 我怎样才能做到这一点?

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))

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). 您可以创建一个新的变量来定义所属组,只需对值进行排序并用数字标记它们(每个变量重复5次)。

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. 注thate的rep ,而不是1:20你应该把n/5之中n元素的数量。

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. 如果需要定义间隔,则只需要使用一组的最大值与以下值中的最小值之间的中位数来定义主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM