change_by {quest} | R Documentation |
Change Scores from a Numeric Vector by Group
Description
change_by
creates a change score (aka difference score) from a numeric
vector separately for each group. It is assumed that the vector is already
sorted within each group by time such that the first element for that group
is earliest in time and the last element for that group is the latest in
time.
Usage
change_by(x, grp, n, undefined = NA)
Arguments
x |
numeric vector. |
grp |
list of atomic vector(s) and/or factor(s) (e.g., data.frame),
which each have same length as |
n |
integer vector with length 1. Specifies how the change score is
calculated. If |
undefined |
atomic vector with length 1 (probably makes sense to be the
same typeof as |
Details
It is recommended to use L
when specifying n
to prevent
problems with floating point numbers. shift_by
tries to circumvent
this issue by a call to round
within shift_by
if n
is
not an integer; however that is not a complete fail safe. The problem is that
as.integer(n)
implicit in shift_by
truncates rather than
rounds. See details of shift_by
.
Value
an atomic vector of the same length as x
that is the change
score by group. If x
and undefined
are different typeofs,
then the return will be coerced to the more complex typoof (i.e., complex
to simple: character, double, integer, logical).
See Also
changes_by
change
changes
shift_by
Examples
change_by(x = ChickWeight[["Time"]], grp = ChickWeight[["Chick"]], n = -1L)
tmp_nm <- c("vs","am") # multiple grouping vectors
change_by(x = mtcars[["disp"]], grp = mtcars[tmp_nm], n = +1L)
tmp_nm <- c("Type","Treatment") # multiple grouping vectors
change_by(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm], n = 2L)