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

Description Usage Arguments Examples

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

1
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

1
2
3
4
5
6
7
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/WWCa documentation built on May 17, 2019, 7:25 a.m.