user1669785
user1669785

Reputation: 113

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.

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.

Upvotes: 1

Views: 3842

Answers (2)

topchef
topchef

Reputation: 19793

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 

Upvotes: 2

agstudy
agstudy

Reputation: 121578

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)

Upvotes: 2

Related Questions