R - Counting elements in a vector within a certain range, as a sliding window?

纵饮孤独 提交于 2020-02-27 12:34:04

问题


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.

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


Here some solutions.

Using cut and 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:

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



回答2:


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 


来源:https://stackoverflow.com/questions/19528926/r-counting-elements-in-a-vector-within-a-certain-range-as-a-sliding-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!