\Header{diff}{Lagged Differences}
\methalias{diff.default}{diff}
\keyword{ts}{diff}
\keyword{arith}{diff}
\begin{Description}\relax
Returns suitably lagged and iterated differences.\end{Description}
\begin{Usage}
\begin{verbatim}
diff(x, ...)
diff.default(x, lag=1, differences=1, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a numeric vector or matrix containing the values to be
differenced.
\item[\code{lag}] an integer indicating which lag to use.
\item[\code{differences}] an integer indicating the order of the difference.
\item[\code{...}] further arguments to be passed to or from methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{diff} is a generic function with a default method and one for
class \code{\Link{ts}} objects.  \code{\Link{NA}}'s propagate.\end{Details}
\begin{Value}
If \code{x} is a vector of length \code{n} and \code{differences=1},
then the computed result is equal to the successive differences
\code{x[(1+lag):n] - x[1:(n-lag)]}.

If \code{difference} is larger than one this algorithm is applied
recursively to \code{x}.
Note that the returned value is a vector which is shorter than
\code{x}.

If \code{x} is a matrix then the difference operations are carried out
on each column separately.\end{Value}
\begin{SeeAlso}\relax
\code{\Link{diff.ts}}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
diff(1:10, 2)
diff(1:10, 2, 2)
x <- cumsum(cumsum(1:10))
stopifnot(diff(x, lag = 2) == x[(1+2):10] - x[1:(10 - 2)],
          diff(x, lag = 2) == (3:10)^2,
          diff(diff(x))    == diff(x, differences = 2))
\end{ExampleCode}
\end{Examples}

