View source: R/utility_functions.R
cast_simple | R Documentation |
data.table
/data.frame
from long format to wide formatConvenience function for using [data.table::dcast.data.table]
;
inputs are character strings (names of variables) instead of a formula.
cast_simple(data = NULL, columns = NULL, rows = NULL, values = NULL)
data |
a |
columns |
a character string vector; the (unique combinations of the) levels of these variable will be different rows |
rows |
a character string vector; the (unique combinations of the) levels of these variable will be different columns |
values |
a character string; the variable which will be represented
on rows and columns as specified by |
This function is just a small interface for dcast
/
dcast.data.table
and less flexible than the originals.
Note that all data.table
objects are also data.frame
objects, but that each have their own dcast
method.
[data.table::dcast.data.table]
is faster.
If any values in value.vars
need to be
aggregated, they are aggregated using sum
.
See ?dcast
.
A data.table
just like [data.table::dcast]
.
Matti Rantanen, Joonas Miettinen
library("data.table")
## e.g. silly counts from a long-format table to a wide format
test <- data.table::copy(popEpi::sire)
test$dg_y <- year(test$dg_date)
test$ex_y <- year(test$ex_date)
tab <- ltable(test, c("dg_y","ex_y"))
cast_simple(tab, columns='dg_y', rows="ex_y", values="obs")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.