Homer Jay Simpson
Homer Jay Simpson

Reputation: 1280

How can I rollapply in R with two columns of a data frame?

I have a data frame that looks like this:

library(tidyverse)
a = seq(-5000,1500,length.out=31);a
b = seq(2000,-5000,length.out=31);b
w = tibble(a,b)%>%base::print(n=31)


# A tibble: 31 × 2
         a      b
     <dbl>  <dbl>
 1 -5000    2000 
 2 -4783.   1767.
 3 -4567.   1533.
 4 -4350    1300 
 5 -4133.   1067.
 6 -3917.    833.
 7 -3700     600 
 8 -3483.    367.
 9 -3267.    133.
10 -3050    -100 
11 -2833.   -333.
12 -2617.   -567.
13 -2400    -800 
14 -2183.  -1033.
15 -1967.  -1267.
16 -1750   -1500 
17 -1533.  -1733.
18 -1317.  -1967.
19 -1100   -2200 
20  -883.  -2433.
21  -667.  -2667.
22  -450   -2900 
23  -233.  -3133.
24   -16.7 -3367.
25   200   -3600 
26   417.  -3833.
27   633.  -4067.
28   850   -4300 
29  1067.  -4533.
30  1283.  -4767.
31  1500   -5000 

I want to check the data frame with sliding window of 11 (starts with the first 11 from top to bottom ) and check the value of "a" column that is the maximum. From my example that will be the value -667.Then stop and keep the new data frame :

21  -667.  -2667.
22  -450   -2900 
23  -233.  -3133.
24   -16.7 -3367.
25   200   -3600 
26   417.  -3833.
27   633.  -4067.
28   850   -4300 
29  1067.  -4533.
30  1283.  -4767.
31  1500   -5000 

from this data frame I want to take the -667 that I previously stopped and the bottom of column b which the -5000 and add them -5000-667=-5667.

How can do it in R ?

Upvotes: 1

Views: 108

Answers (1)

akrun
akrun

Reputation: 887153

We loop over the sequence of rows in slide, with .after = 11, slice the rows of 'w' where the value of 'a' is max, get the corresponding index for slicing, use .complete = TRUE to return only those having complete set of 11 index or else it will be NULL, _dfr will return a tibble from the sliced data and then use slice_tail to return the last 11 rows

library(slider)
library(dplyr)
library(purrr)
n1 <- 11
seq_len(nrow(w)) %>%
    slide_dfr(~ {
      ind <- .x
     w %>% 
     slice(ind[which.max(a[.x])])
    }, .after = n1, .complete = TRUE) %>%
   slice_tail(n = n1)

-output

# A tibble: 11 × 2
        a      b
    <dbl>  <dbl>
 1 -667.  -2667.
 2 -450   -2900 
 3 -233.  -3133.
 4  -16.7 -3367.
 5  200   -3600 
 6  417.  -3833.
 7  633.  -4067.
 8  850   -4300 
 9 1067.  -4533.
10 1283.  -4767.
11 1500   -5000 

Upvotes: 2

Related Questions