\HeaderA{diff}{Lagged Differences}{diff}
\methaliasA{diff.Date}{diff}{diff.Date}
\methaliasA{diff.default}{diff}{diff.default}
\methaliasA{diff.POSIXt}{diff}{diff.POSIXt}
\keyword{arith}{diff}
\begin{Description}\relax
Returns suitably lagged and iterated differences.
\end{Description}
\begin{Usage}
\begin{verbatim}
diff(x, ...)

## Default S3 method:
diff(x, lag = 1, differences = 1, ...)

## S3 method for class 'POSIXt':
diff(x, lag = 1, differences = 1, ...)

## S3 method for class 'Date':
diff(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 ones for
classes \code{"\LinkA{ts}{ts}"}, \code{"\LinkA{POSIXt}{POSIXt}"} and
\code{"\LinkA{Date}{Date}"}.

\code{\LinkA{NA}{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{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{diff.ts}{diff.ts}}, \code{\LinkA{diffinv}{diffinv}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
diff(1:10, 2)
diff(1:10, 2, 2)
x <- cumsum(cumsum(1:10))
diff(x, lag = 2)
diff(x, differences = 2)

diff(.leap.seconds)
\end{ExampleCode}
\end{Examples}

