Compute first differences
Variable to be first-differenced.
Optional grouping variable (see 'Details').
Optional data frame containing
df is specified then
group must also be specified. So it is possible
to specify all three parameters,
group only, or
var by itself.
An example of when one might wish to omit both
df is when using
in conjunction with plyr's
ddply (see 'Examples'). If
df is specified then it
should be sorted by
group and, if necessary, a second variable (e.g. time) that orders the
var in the appropriate sequence.
firstD(var) returns a first-differenced version of
firstD(var,group) returns a first-differenced version of
firstD(var,group,df) returns a first-differenced version of
group are searched for in
df. Note that the first value of
firstD(var) will be
NA since there is no difference to compute. Similarly, for
firstD(var,group,df) the first value for each group
Jonah Gabry <[email protected]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Specifying both group and df df <- data.frame(id = rep(1:3, each = 3), X = rpois(9, 10)) df$Xdiff <- firstD(X, id, df) df # Omitting df id <- rep(1:3, each = 3) X <- rpois(9, 10) Xdiff <- firstD(X, id) # Omitting group and df ## Not run: library(plyr) df <- data.frame(id = rep(1:3, each = 3), X = rpois(9, 10), Y = rpois(9, 5)) ddply(df, "id", mutate, Xdiff = firstD(X), Ydiff = firstD(Y)) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.