dat = structure(list(index = c(10505L, 10506L, 10511L, 10539L, 10542L,
10579L, 10642L, 11008L, 11012L, 13011L, 13110L, 13116L, 13118L,
13156L, 13259L, 13273L, 13313L, 13365L, 13380L, 13382L, 13445L,
13453L, 13482L, 13483L, 13494L, 13543L, 13550L, 14462L, 14464L,
14564L, 14599L, 14604L, 14674L, 14719L, 14728L, 14775L, 14860L,
14874L, 14930L, 14933L, 14975L, 15031L, 15089L, 15117L, 15179L,
15211L, 15241L, 15245L, 15255L, 15260L, 15418L, 15585L, 15627L,
15644L, 15774L, 15776L, 15777L, 15790L, 15791L, 15833L, 15849L,
15850L, 15886L, 16042L, 16127L, 16140L, 16141L, 16142L, 16365L,
16485L, 16489L, 16515L, 16542L, 16738L, 16834L, 16949L, 17272L,
17462L, 17569L, 17571L, 17641L, 17654L, 17694L, 17695L, 17709L,
17748L, 17836L, 17922L, 18643L, 20113L, 20131L, 28914L, 29318L,
30524L, 30741L, 30912L, 30923L, 30998L, 46650L, 46698L), V2 = c(3L,
3L, 3L, 2L, 2L, 2L, 2L, 1L, 0L, 3L, 2L, 2L, 2L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 0L, 1L, 2L, 2L, 2L, 2L, 1L, 0L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L,
0L, 0L, 0L, 2L, 3L, 5L, 3L, 0L, 0L, 3L, 1L, 0L, 3L, 0L, 0L, 2L,
1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 2L, 1L, 0L, 0L, 1L, 0L, 0L, 0L,
1L, 1L, 1L)), row.names = c(NA, -100L), class = "data.frame")
Let's say I want to calculate a function across dat
in a rolling window.
n_sites = function(x) {
return(sum(x > 1))
}
zoo::rollapply(dat$V2, FUN=n_sites, width=100)
However, rather than using the number of rows as the window size, I'd like to use the actual numeric values in the index
column. So I would like to have each window so that it includes roughly 100 units in the index column. Given there is roughly 100 units of index
between the 1st and 7th rows, the first window would include these rows. Is this possible?
Happy for a solution using zoo
or data.table
or the like.