I have data frame in R that looks like this :
| a | b |
|---|---|
| 8 | -16 |
| 19 | -26 |
| 30 | -36 |
| 41 | -46 |
| 52 | -56 |
I want to slide it or rollapply it with width 3 in both columns and calculate the the sum of the two minimum.
BUT.!!!!
I want progressively go tho width 3 starting with width (3+1)/2 = 2 and then go to width 3.
In my example must start with the first 2 rows:
| a | b |
|---|---|
| 8 | -16 |
| 19 | -26 |
result must be the sum of the minimums of columns a and b 8+(-26)=-18 next
| a | b |
|---|---|
| 8 | -16 |
| 19 | -26 |
| 30 | -36 |
result must be the sum of the minimums of columns a and b 8+(-36)=-28
next
| a | b |
|---|---|
| 19 | -26 |
| 30 | -36 |
| 41 | -46 |
19-46 = -27
next
| a | b |
|---|---|
| 30 | -36 |
| 41 | -46 |
| 52 | -56 |
30-56 = -26
and last
| a | b |
|---|---|
| 41 | -46 |
| 52 | -56 |
41-56=-15.
The width must be 2,3,3,3,2.
In general if this data frame had 100 rows with window 13 it will start from top to bottom with window (or width) (13+1)/2 = 7, then it will continue to 8,9,10,12,13,13,...,13,12,11,10,9,8,7.
How can I do this in R ?
library(tidyverse)
a = c(800,1900,3000,4100,5200)
b = c(-1600,-2600,-3600,-4600,-5600)
w = tibble(a,b)
We may use
rollapplyfromzoowithpartial = TRUEto loop over the sequence of rows, subset the data based on the index, get theminfrom columns (sapply(..., min)) and get thesumof thosedata