seqAgg: Sequential Aggregation

Description Usage Arguments Details Examples

View source: R/seqAgg.R

Description

Sequentially aggregate columns in a data.table, varying the function and 'by' values

Usage

1
seqAgg(X, val, FUN, by, ...)

Arguments

X

a data.table

val

character vector indicating columns in X that should be sequuentially aggregated

FUN

a list of functions

by

a list of character vectors; should be of same length as FUN

...

arguments to be passed to each element of FUN

Details

The lengths of FUN and by should be of the same length, but the length of val is independent.

Useful when a summary statistic needs to be computed sequentially (possibly taking the mean over some dimensions before computing the standard deviation, e.g.)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
neus_dat <- data_all[reg=="neus"&year==2010]
a1 <- c("K","stratum")
a2 <- c("stratum")
a3 <- c("")
a_list <- list(a1, a2, a3)
f_list <- list(mean, mean, sd)

neus_dat[,sd(btemp,na.rm=TRUE)] # naive
seqAgg(neus_dat, val="btemp", FUN=f_list, by=a_list, na.rm=TRUE) # sequential
seqAgg(neus_dat, val=c("btemp","depth"), FUN=f_list, by=a_list, na.rm=TRUE) # 2 columns

rBatt/trawlDiversity documentation built on Aug. 14, 2021, 1:01 p.m.