rtidyversetsibble

R: diff() and tsibble - must be size n or 1 not n-2


Given:

test <- tsibble(
  date = lubridate::now() + 0:9,
  value = rnorm(10)
)  %>%
  mutate (vdiff =diff(value, differences = 2))

How do I avoid the following :

Error: Problem with mutate() column vdiff. ℹ vdiff = diff(value, differences = 2, na.pad = TRUE). ℹ vdiff must be size 10 or 1, not 8

I tried adding na.pad = TRUE but that has no effect (and in any event, I would prefer to pad with 0 not NA)


Solution

  • One approach to achieve your desired result would be to make use of dplyr::lag. However thanks to the comment by @MitchellO'Hara-Wild there is an easier way to achieve this by simply making use of tsibble::difference

    library(tsibble)
    library(dplyr)
    
    set.seed(42)
    
    d <- tsibble(
      date = lubridate::now() + 0:9,
      value = rnorm(10)
    )  
    #> Using `date` as index variable.
    
    d %>%
      mutate(vdiff = difference(value, differences = 2, default = 0))
    #> # A tsibble: 10 x 3 [1s] <?>
    #>    date                  value  vdiff
    #>    <dttm>                <dbl>  <dbl>
    #>  1 2021-06-20 14:33:28  1.37    0    
    #>  2 2021-06-20 14:33:29 -0.565   0    
    #>  3 2021-06-20 14:33:30  0.363   2.86 
    #>  4 2021-06-20 14:33:31  0.633  -0.658
    #>  5 2021-06-20 14:33:32  0.404  -0.498
    #>  6 2021-06-20 14:33:33 -0.106  -0.282
    #>  7 2021-06-20 14:33:34  1.51    2.13 
    #>  8 2021-06-20 14:33:35 -0.0947 -3.22 
    #>  9 2021-06-20 14:33:36  2.02    3.72 
    #> 10 2021-06-20 14:33:37 -0.0627 -4.19