Skip to contents

Difference a "timeSeries" object.

Usage

# S3 method for timeSeries
diff(x, lag = 1, diff = 1, trim = FALSE, pad = NA, ...)

Arguments

x

an object of class "timeSeries".

lag

an integer indicating which lag to use.

diff

an integer indicating the order of the difference.

trim

a logical flag. Should NAs at the beginning of the series be removed?

pad

a numeric value with which NAs should be replaced at the beginning of the series.

...

currently not used.

Value

the differenced "timeSeries" object

See also

diff for base::diff, lag

Examples

## load Microsoft dataset
x <- MSFT[1:12, ]
x
#> GMT 
#>               Open    High     Low   Close   Volume
#> 2000-09-27 63.4375 63.5625 59.8125 60.6250 53077800
#> 2000-09-28 60.8125 61.8750 60.6250 61.3125 26180200
#> 2000-09-29 61.0000 61.3125 58.6250 60.3125 37026800
#> 2000-10-02 60.5000 60.8125 58.2500 59.1250 29281200
#> 2000-10-03 59.5625 59.8125 56.5000 56.5625 42687000
#> 2000-10-04 56.3750 56.5625 54.5000 55.4375 68226700
#> 2000-10-05 55.5000 57.2500 55.2500 55.3750 40549700
#> 2000-10-06 55.8125 56.7500 54.7500 55.5625 30897000
#> 2000-10-09 55.6250 55.7500 53.0000 54.1875 29161800
#> 2000-10-10 53.9375 55.5625 53.8125 54.5625 31033100
#> 2000-10-11 54.0000 56.9375 54.0000 55.7500 50602900
#> 2000-10-12 56.3125 56.8750 53.8125 54.3750 45109800

## compute differences
diff(x)   
#> GMT 
#>               Open    High     Low   Close    Volume
#> 2000-09-27      NA      NA      NA      NA        NA
#> 2000-09-28 -2.6250 -1.6875  0.8125  0.6875 -26897600
#> 2000-09-29  0.1875 -0.5625 -2.0000 -1.0000  10846600
#> 2000-10-02 -0.5000 -0.5000 -0.3750 -1.1875  -7745600
#> 2000-10-03 -0.9375 -1.0000 -1.7500 -2.5625  13405800
#> 2000-10-04 -3.1875 -3.2500 -2.0000 -1.1250  25539700
#> 2000-10-05 -0.8750  0.6875  0.7500 -0.0625 -27677000
#> 2000-10-06  0.3125 -0.5000 -0.5000  0.1875  -9652700
#> 2000-10-09 -0.1875 -1.0000 -1.7500 -1.3750  -1735200
#> 2000-10-10 -1.6875 -0.1875  0.8125  0.3750   1871300
#> 2000-10-11  0.0625  1.3750  0.1875  1.1875  19569800
#> 2000-10-12  2.3125 -0.0625 -0.1875 -1.3750  -5493100
   
## trimmed differences
diff(x, trim = TRUE) 
#> GMT 
#>               Open    High     Low   Close    Volume
#> 2000-09-28 -2.6250 -1.6875  0.8125  0.6875 -26897600
#> 2000-09-29  0.1875 -0.5625 -2.0000 -1.0000  10846600
#> 2000-10-02 -0.5000 -0.5000 -0.3750 -1.1875  -7745600
#> 2000-10-03 -0.9375 -1.0000 -1.7500 -2.5625  13405800
#> 2000-10-04 -3.1875 -3.2500 -2.0000 -1.1250  25539700
#> 2000-10-05 -0.8750  0.6875  0.7500 -0.0625 -27677000
#> 2000-10-06  0.3125 -0.5000 -0.5000  0.1875  -9652700
#> 2000-10-09 -0.1875 -1.0000 -1.7500 -1.3750  -1735200
#> 2000-10-10 -1.6875 -0.1875  0.8125  0.3750   1871300
#> 2000-10-11  0.0625  1.3750  0.1875  1.1875  19569800
#> 2000-10-12  2.3125 -0.0625 -0.1875 -1.3750  -5493100

## padded differences
diff(x, trim = FALSE, pad = 0) 
#> GMT 
#>               Open    High     Low   Close    Volume
#> 2000-09-27  0.0000  0.0000  0.0000  0.0000         0
#> 2000-09-28 -2.6250 -1.6875  0.8125  0.6875 -26897600
#> 2000-09-29  0.1875 -0.5625 -2.0000 -1.0000  10846600
#> 2000-10-02 -0.5000 -0.5000 -0.3750 -1.1875  -7745600
#> 2000-10-03 -0.9375 -1.0000 -1.7500 -2.5625  13405800
#> 2000-10-04 -3.1875 -3.2500 -2.0000 -1.1250  25539700
#> 2000-10-05 -0.8750  0.6875  0.7500 -0.0625 -27677000
#> 2000-10-06  0.3125 -0.5000 -0.5000  0.1875  -9652700
#> 2000-10-09 -0.1875 -1.0000 -1.7500 -1.3750  -1735200
#> 2000-10-10 -1.6875 -0.1875  0.8125  0.3750   1871300
#> 2000-10-11  0.0625  1.3750  0.1875  1.1875  19569800
#> 2000-10-12  2.3125 -0.0625 -0.1875 -1.3750  -5493100