data(acme)
#Aggregate on a field
Aggregate(acme, "cost", sum)
#This is the same as:
HomeRolledAggregate <- function(node) {
sum(sapply(node$children, function(child) {
if (!is.null(child$cost)) child$cost
else HomeRolledAggregate(child)
}))
}
HomeRolledAggregate(acme)
#Aggregate using Get
print(acme, "cost", minCost = acme$Get(Aggregate, "cost", min))
#use Aggregate with a function:
Aggregate(acme, function(x) x$cost * x$p, sum)
#cache values along the way
acme$Do(function(x) x$cost <- Aggregate(x, "cost", sum), traversal = "post-order")
acme$IT$cost
Run the code above in your browser using DataLab