1

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)

Little Code
  • 1,315
  • 2
  • 16
  • 37

1 Answers1

1

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
stefan
  • 90,330
  • 6
  • 25
  • 51
  • You could also use the `difference()` function which pads the values with NA. – Mitchell O'Hara-Wild Jun 20 '21 at 12:21
  • @MitchellO'Hara-Wild. Thanks for pointing that out. Wasn't aware of that a I'm not familiar with the `tsibble` package. Even more problematic, my approach actually results in the wrong result as I missed the difference between "lag" and "differences". – stefan Jun 20 '21 at 12:30