Often when creating lagged differences, it is awkward that the differences vector is shorter than the original. ediff pads with pad.value to make its output the same length as the input.

ediff(
  x,
  lag = 1,
  differences = 1,
  pad = c("head", "tail", "symmetric"),
  pad.value = NA,
  frontPad,
  ...
)

Arguments

x

a numeric vector or a matrix containing the values to be differenced

lag

an integer indicating which lag to use

differences

an integer indicating the order of the difference

pad

one of "head", "tail", or "symmetric". indicating where the pad.value padding should be added to the result.

pad.value

the value to be used for padding.

frontPad

logical indicating whether padding is on the front (head) or back (tail) end. This exists for backward compatibility. New code should use pad instead.

...

further arguments to be passed to or from methods

See also

diff() since ediff is a thin wrapper around diff().

Examples

ediff(1:10)
#>  [1] NA  1  1  1  1  1  1  1  1  1
ediff(1:10, pad.value = 0)
#>  [1] 0 1 1 1 1 1 1 1 1 1
ediff(1:10, 2)
#>  [1] NA NA  2  2  2  2  2  2  2  2
ediff(1:10, 2, 2)
#>  [1] NA NA NA NA  0  0  0  0  0  0
x <- cumsum(cumsum(1:10))
ediff(x, lag = 2)
#>  [1]  NA  NA   9  16  25  36  49  64  81 100
ediff(x, differences = 2)
#>  [1] NA NA  3  4  5  6  7  8  9 10
ediff(x, differences = 2, pad = "symmetric")
#>  [1] NA  3  4  5  6  7  8  9 10 NA
ediff(.leap.seconds)
#>  [1]   NA  184  365  365  365  366  365  365  365  547  365  365  731  914  731
#> [16]  365  547  365  365  549  547  549 2557 1096 1277 1095  550