KostraApply: Apply a function with data frame output over data subsets

View source: R/KostraApply.R

KostraApplyR Documentation

Apply a function with data frame output over data subsets

Description

Apply a function with data frame output over data subsets

Usage

KostraApply(data, by, Fun, ...)

Arguments

data

Data frame

by

Vector of variable names (or variable numbers) defining the subsets or alternatively a named list or data frame (see examples).

Fun

Function to be applied on data subsets

...

Arguments passed to Fun

Value

A data.frame

Note

The output ordering of the example depends on getOption("stringsAsFactors")

Examples

z1w <- KostraData("z1w")
zz  <- rbind(cbind(aar=2014,z1w),cbind(aar=2015,z1w))
zzz <- rbind(cbind(ab="b",zz),cbind(ab="a",zz))
zzz$annet[c(1,9,17,25)] =10*(1:4)

# All subset in data by vector input
KostraApply(zzz, by = c("aar", "ab"), Fun = ProtectKostra, idVar = "region", freqVar = 4:7)

# Selected subsets by using list input to specify each variable
KostraApply(zzz, by = list(aar = c(2014, 2015), ab = "a"), Fun = ProtectKostra, idVar = "region", freqVar = 4:7)

# Or use NULL when all values in data
KostraApply(zzz, by = list(aar = NULL, ab = "a"), Fun = ProtectKostra, idVar = "region", freqVar = 4:7)

# Selected subsets by using data frame input to specify variable combinations
KostraApply(zzz, by = data.frame(aar = c(2014, 2015), ab = c("b", "a")), Fun = ProtectKostra, idVar = "region", freqVar = 4:7)


statisticsnorway/Kostra documentation built on Sept. 25, 2024, 10:37 a.m.