[英]R - Counting elements in a vector within a certain range, as a sliding window?
I am using R and I would like to convert a standard vector of integers into a 2-column data frame showing the the number of elements in each vector that fall within a specified-sized window. 我正在使用R,我想将整数的标准向量转换为2列数据框,显示每个向量中属于指定大小窗口的元素数。
For instance take this vector: 例如,拿这个向量:
1, 75, 79, 90, 91, 92, 109, 120, 167, 198, 203, 204, 206, 224, 230,
236, 240, 245, 263, 344
The results for looking at the values that fall with in a window-size of 50 should look like this: 查看窗口大小为50的值的结果应如下所示:
50 1
100 5
150 2
200 2
250 8
300 1
350 1
400 0
With the first column as the number range, and the second as the count within that range. 第一列为数字范围,第二列为该范围内的计数。
This shows that for the range of 1-50 there is 1 element in that range, for 51-100 there are 5 elements, for 101-150 there are 2 elements, etc. It is key, however, that the window-size be flexible, as I will use this for multiple analyses. 这表明,对于1-50的范围,在该范围内有1个元素,对于51-100有5个元素,对于101-150有2个元素等。然而,关键是窗口大小是灵活,因为我将这用于多个分析。
Here some solutions. 在这里有一些解决
Using cut
and table
: 使用cut
和table
:
table(cut(vv,seq(min(vv),max(vv),50),include.lowest = TRUE))
[1,51] (51,101] (101,151] (151,201] (201,251] (251,301]
1 5 2 2 8 1
Using findInterval
: 使用findInterval
:
table(findInterval(vv,seq(min(vv),max(vv),50)))
1 2 3 4 5 6 7
1 5 2 2 8 1 1
Using shingle
from lattice package: 使用格子包装中的shingle
:
shingle(vv,cbind(seq(min(vv),max(vv),50) ,seq(50,max(vv)+50,50)))
Intervals:
min max count
1 1 50 1
2 51 100 5
3 101 150 2
4 151 200 2
5 201 250 8
6 251 300 1
7 301 350 1
where `vv`:
c(1, 75, 79, 90, 91, 92, 109, 120, 167, 198, 203, 204, 206, 224,
230, 236, 240, 245, 263, 344)
table((as.integer(d/50)+1) * 50)
or using integer divide 或使用整数除法
table((d%/%50+1) * 50)
that outputs: 输出:
50 100 150 200 250 300 350
1 5 2 2 8 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.