varcalc_dt: Execute *vertical* calculations along a given column.

View source: R/helpers.R

varcalc_dtR Documentation

Execute *vertical* calculations along a given column.

Description

This assumes a *long* format with a single value column, dcasts the data.table to wide format, executes the calulation(s), melts back to long format and returns the resulting data.table with the additional column(s).

Usage

varcalc_dt(dt, varcol, valcol, expr, ...)

Arguments

dt

data.table, long format

varcol

name of the column with the variable

valcol

name of the column with the value

expr

vector of expressions to be handed to j in data.table, as strings, e.g., "a := b/c"

...

other arguments are passed on to the data.table call where 'expr' is evaluated. Most likely you want to pass the 'by=' parameter for group-by calls, see examples.

Details

Note that the data.table should have at least three columns, i.e., the variable, the value and one id column.

Examples

mt_dt <- as.data.table(mtcars, keep.rownames = TRUE)
## to long
mt1 <- melt(mt_dt, id.vars=c("rn", "cyl"))

varcalc_dt(mt1, "variable", "value", c("`spec. hp` := wt/hp", "wsum := sum(wt)"), by="cyl")

pik-piam/rmndt documentation built on April 21, 2024, 4:31 a.m.