Skip to contents

An extension on `diff` from base R that fills blank space with NA

Usage

diffFill(x, lag = 1, differences = 1, name = NULL, ...)

Arguments

x

a numeric vector OR matrix containing the values to be differenced

lag

(Optional) an integer indicating which lag to use. If `x` is a vector, `lag` can also be a vector. Defaults to 1

differences

(Optional) an integer indicating the order of the difference. If `x` is a vector, `differences` can also be a vector. Defaults to 1

name

(Optional) A name to be used in the lagged data.frame. Defaults to the name of the variable passed to `x`. If that is not possible, `name` will default to "X".

...

(Optional) further arguments to be passed to or from methods

Value

Returns a vector of the same length as the input, otherwise behaves the same as diff. If the input is a matrix, it returns a matrix.

Examples

# Creating dummy data
# for 1 lag and 2 differences
diffFill(rnorm(10), 1, 2)
#>        x_l1d2
#> 1          NA
#> 2          NA
#> 3   3.6436427
#> 4  -4.4990801
#> 5   1.1338189
#> 6   0.4220543
#> 7   0.3251422
#> 8   1.7209838
#> 9  -1.8293894
#> 10 -1.2955908