Learn R Programming

popEpi (version 0.2.1)

cast_simple: Cast data.table/data.frame from long format to wide format

Description

Convenience function for using dcast.data.table and link[reshape2]{dcast}; inputs are character strings (names of variables) instead of a formula.

Usage

cast_simple(data = NULL, columns = "year", rows = c("PrimarySite", "sex"),
  values = "std.incidence")

Arguments

data
a data.table or data.frame
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 columns and rows

Details

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 alos data.frame objects, but that each have their own dcast method. dcast.data.table is faster. If any information needs to aggregated, it is aggregated using sum.

Examples

Run this code
## e.g. silly counts from a long-format table to a wide format
test <- copy(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")

Run the code above in your browser using DataLab