Create an Object of Differences

DESCRIPTION:
Returns a time series or other object which is the result of differencing the input.

USAGE:
diff(x, lag=1, differences=1)

REQUIRED ARGUMENTS:
x:
a univariate or multivariate time series, a vector, or a matrix. Missing values (NAs) are allowed.

OPTIONAL ARGUMENTS:
lag:
integer specifying the lag of the difference to be computed. Non-integer values are rounded to the nearest integer. The default is 1, that is, the difference between successive elements is taken.
differences:
integer specifying the number of differences to be done. Non-integer values are rounded to the nearest integer. The default is 1.

VALUE:
a vector, matrix or time series which is the nth difference of lag k for x. If y <- diff(x,k), y[i] is x[i]-x[i-k], and length(y) is length(x)-k. In a time series, the first k times are dropped. To construct an nth difference, the procedure is iterated n times.

DETAILS:
This is a generic function. Methods exist for the time series classes "rts", and "cts" and for objects of class "dates".

Any operation on an NA produces an NA. For matrices, the differences are performed on each column separately.


SEE ALSO:
lag , Arithmetic .

EXAMPLES:
# second difference of lag 4:
# the start date of d2x will be
# 8 periods later than x
  freeny.rts<- as.rts(freeny.y)
  freeny.df2 <- diff(freeny.rts, 1,2)

# 10th difference of an alternating series diff(rep(c(-1,1),10), 1, 10) [1] -1024 1024 -1024 1024 -1024 1024 -1024 1024 [9] -1024 1024

diff(range(x)) # max(x) - min(x)