1

I'd like to pass a rolling window of some columns a custom function, with the actual value of other columns.

Given the example data, and the example function. Apply myfunc to rolling windows, of size 3, of the variables var1 and var2, and the first values of the param1 and param2.

Example: For the row 2015-07-03 the myfunc function is passed:

  • var1=c(1.18,1.27, 1.36)
  • param1=3
  • var2=c(3.55,3.82,4.09)
  • param2=13

The example data

library(dplyr)

myfunc <- function(var1, param1, var2, param2){
  c(length(var1), length(var2), param1, param2)
}

d <- data_frame(date = seq(as.Date('2015-07-01'), as.Date('2015-07-12'), by = '1 day'))
d <- d %>%
  mutate(var1   = seq(1,2, length=12), 
         var2   = seq(3,6, length=12),
         param1 = rep(seq(1,3, length=3),4),
         param2 = rep(seq(11,13, length=3),4))

>d
  # A tibble: 12 x 5
  date       param1 param2  var1  var2
<date>      <dbl>  <dbl> <dbl> <dbl>
  1 2015-07-01      1     11  1     3   
2 2015-07-02      2     12  1.09  3.27
3 2015-07-03      3     13  1.18  3.55
4 2015-07-04      1     11  1.27  3.82
5 2015-07-05      2     12  1.36  4.09
6 2015-07-06      3     13  1.45  4.36
7 2015-07-07      1     11  1.55  4.64
8 2015-07-08      2     12  1.64  4.91
9 2015-07-09      3     13  1.73  5.18
10 2015-07-10      1     11  1.82  5.45
11 2015-07-11      2     12  1.91  5.73
12 2015-07-12      3     13  2     6   

Desired output:

# A tibble: 12 x 4
date       param1 param2  res  
<date>      <dbl>  <dbl> <lst> 
1 2015-07-01      1     11  <..>     
2 2015-07-02      2     12  <..>   
3 2015-07-03      3     13  <..>   
4 2015-07-04      1     11  <..>   
5 2015-07-05      2     12  <..>   
6 2015-07-06      3     13  <..>  
7 2015-07-07      1     11  <..>   
8 2015-07-08      2     12  <..>   
9 2015-07-09      3     13  <..>  
10 2015-07-10      1     11  <..>   
11 2015-07-11      2     12  <..>   
12 2015-07-12      3     13  <..>     

where for the row 2015-07-03 the content of d$res is 3,3,3,13

1
  • Isn't length(var1) and var2 always going to be 3? Is your rolling window size 3 by 1 step? Commented Feb 8, 2019 at 7:35

1 Answer 1

0

Here is an idea that uses the rollapply function from zoo package. I made a slight modification to your function by removing the length (which basically makes the function unnecessary but I will use it just for reference) since I am doing that via rollapply, i.e.

 myfunc <- function(var1, param1, var2, param2) {
     c(var1, var2, param1, param2)
 }

library(zoo)
library(tidyverse)

d %>% 
 mutate(newvar1 = rollapply(var1, width = 3, FUN = length, partial = TRUE), 
        newvar2 = rollapply(var2, width = 3, FUN = length, partial = TRUE)) %>% 
 rowwise() %>% 
 mutate(res = list(myfunc(newvar1, newvar2, param1, param2)))
 #or mutate(res = list(c(newvar1, ...)))

which gives,

# A tibble: 12 x 8
   date        var1  var2 param1 param2 newvar1 newvar2 res      
   <date>     <dbl> <dbl>  <dbl>  <dbl>   <int>   <int> <list>   
 1 2015-07-01  1     3         1     11       2       2 <dbl [4]>
 2 2015-07-02  1.09  3.27      2     12       3       3 <dbl [4]>
 3 2015-07-03  1.18  3.55      3     13       3       3 <dbl [4]>
 4 2015-07-04  1.27  3.82      1     11       3       3 <dbl [4]>
 5 2015-07-05  1.36  4.09      2     12       3       3 <dbl [4]>
 6 2015-07-06  1.45  4.36      3     13       3       3 <dbl [4]>
 7 2015-07-07  1.55  4.64      1     11       3       3 <dbl [4]>
 8 2015-07-08  1.64  4.91      2     12       3       3 <dbl [4]>
 9 2015-07-09  1.73  5.18      3     13       3       3 <dbl [4]>
10 2015-07-10  1.82  5.45      1     11       3       3 <dbl [4]>
11 2015-07-11  1.91  5.73      2     12       3       3 <dbl [4]>
12 2015-07-12  2     6         3     13       2       2 <dbl [4]>

where for the 3rd row,

d2$res[3]
#[[1]]
#[1]  3  3  3 13

NOTE: You can add a select statement at the end and of the pipe and remove newvar* columns

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.