up_apply: Apply a function to clusters of rows in a data frame

up_applyR Documentation

Apply a function to clusters of rows in a data frame

Description

Apply a function to clusters of rows in a data frame and return the result so it is conformable with the data frame created by 'up' applied to the same data frame and same clustering formula

Usage

up_apply(object, form, FUN, ..., sep = "/")

Arguments

object

a data frame as source for an aggregated result

form

a one-sided formula identifying the variable(s) in object that identifies clusters. e.g. ~ school/Sex to get a summary within each Sex of each school

FUN

a function to be applied to each data frame consisting of a cluster of rows of 'object'. The most common choice is with so that '...' can be an expression using variable names in 'object'.

...

other arguments to FUN, frequently when FUN is 'with', an expression using variable names in 'object'

Examples

zd <- data.frame(a=c('a','a','b','b','c','c','c'),
      b = c("B","B","A","B","C","D","D"), x = 1:7, y = 11:17)
zd$n <- capply(zd$x, zd[c('a','b')], length)
zdu <- up(zd, ~a, agg = ~b)
zdu
zdu$p <- up_apply(zd, ~ a, with, sum(x)/sum(y))
zdu

gmonette/spida2 documentation built on Aug. 11, 2024, 7:52 p.m.