StrataApply: INTERNAL FUNCTION: Apply a function with list output over...

View source: R/StrataApply.R

StrataApplyR Documentation

INTERNAL FUNCTION: Apply a function with list output over data subsets and structure output into various matrices.

Description

INTERNAL FUNCTION: Apply a function with list output over data subsets and structure output into various matrices.

Usage

StrataApply(
  data,
  by,
  Fun,
  copyVar = NULL,
  byName = NULL,
  byNameDefault = "strata",
  multiUnlist = FALSE,
  FunTotal = NULL,
  FunMultiTotal = NULL,
  ...
)

Arguments

data

Data frame

by

Separate vector or Variable in data (name or number) defining the subsets (strata)

Fun

Function to be applied on data subsets

copyVar

Variables from input data to be directly copied to output

byName

Name to be used in output of variable defining subsets

byNameDefault

Name to be used when byName=NULL and when name cannot be taken from input variable

multiUnlist

When TRUE output elements "multi*" are split as separate elements.

FunTotal

A function to be applied on output element "aggregates" so that output element "total" is produced

FunMultiTotal

A function to be applied on output element "multiAggregates" so that output element "multiTotal" is produced

...

Further arguments passed to Fun

Value

Output is a list with elements:

micro

Matrix with as many rows as data - composed of vector output.

aggregates

Matrix with as many rows as the number of subsets - composed of scalar output.

total

Result of FunTotal (typically a matrix with a single row).

multiMicro

List of matrices with as many rows as data - composed of matrix output.

multiAggregates

List of matrices with as many rows as the number of subsets - composed of single row matrix output.

multiTotal

Result of FunMultiTotal (typically list of matrices with a single row).

other

List of output that did not fit into other elements.

Empty elements are removed from output. Elements may also be changed due to multiUnlist.

Examples

 FunFun = function(data,x){
 list(max=max(data[,x]),plus100=data[,x]+100,range=t(range(data[,x])),
      xsqrtx5=cbind(data[,x],sqrt(data[,x]),5),onefive=t(1:5))}
 z = data.frame(x=(1:10)^2,k=c(1,2,2,3,3,3,4,4,4,4),six=rep(6,10))
 FunFun(z,"x")
 StrataApply(z,"k",FunFun,copyVar="six",x="x")
 StrataApply(z,"k",FunFun,copyVar="six",x="x",multiUnlist=TRUE)

statisticsnorway/Kostra documentation built on Nov. 2, 2024, 6:40 p.m.