
Last chance! 50% off unlimited learning
Sale ends in
A function to conveniently stack groups of wide columns into a long form
which can then be merge
d together.
Stacked(data, id.vars = NULL, var.stubs, sep, keep.all = TRUE,
keyed = TRUE, keep.rownames = FALSE, ...)
The source data.frame
.
The variables that serve as unique identifiers. Defaults to NULL
, at which point, all names which are not identified as variable groups are used as the identifiers.
The prefixes of the variable groups.
The character that separates the "variable name" from the "times"
in the wide data.frame
. Alternatively, can be set to
"var.stubs"
(in quotes) if you do not have a value for sep
.
Logical. Should all the variables from the source
data.frame
be kept (keep.all = TRUE
) or should the resulting
data.table
comprise only columns for
the id.vars
, var.stubs
, and "times" (keep.all = FALSE
).
Other variables are recycled to appropriate length. For this to work,
both id.vars
and var.stubs
must be specified.
Logical. Should the Stacked
function automatically set
the key
for the resulting data.table
s. If TRUE
(default) the key
is set to the id.vars
and the "time"
variables that are created by Stacked
.
Logical. Should rownames be kept when converting the input to a data.table
? Defaults to FALSE
.
Other arguments to be passed on when sep = "var.stubs"
(specifically, atStart
: A logical argument to indicate whether the stubs come at the start or at the end of the variable names).
A list
of data.table
s with one data.table
for
each "var.stub". The key
is set to the
id.vars
and .time_#
vars.
# NOT RUN {
set.seed(1)
mydf <- data.frame(id_1 = 1:6, id_2 = c("A", "B"),
varA.1 = sample(letters, 6),
varA.2 = sample(letters, 6),
varA.3 = sample(letters, 6),
varB.2 = sample(10, 6),
varB.3 = sample(10, 6),
varC.3 = rnorm(6))
mydf
Stacked(data = mydf, var.stubs = c("varA", "varB", "varC"), sep = ".")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab