up_apply | R Documentation |
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
up_apply(object, form, FUN, ..., sep = "/")
object |
a data frame as source for an aggregated result |
form |
a one-sided formula identifying the variable(s) in |
FUN |
a function to be applied to each data frame consisting of a
cluster of rows of 'object'. The most common choice is |
... |
other arguments to FUN, frequently when FUN is 'with', an expression using variable names in 'object' |
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.