by is equivalent to "group by" in SQL language. It groups the
data according the value(s) of one or multiple columns, and then apply
an aggregate function onto each group of the data.
A list of
A function, which will be applied onto each group of the data. The result of
Extra arguments passed to
Not implemented yet.
The type of the returned value depends on the return type of
If the return type of
FUN is a
db.obj object, then this function returns
db.Rquery object, which is actually the SQL query that does
the "GROUP BY". It computes the group-by values. The result can be
If the return type of
FUN is not a
db.obj object, then this function returns a list, which contains a number of sub-lists. Each sub-list contains two items: (1)
index, an array of strings, a set of distinct values of the
INDICES converted to string; and (2)
result, the result produced by
FUN applying onto the group of data that has the set of distinct values. The total number of sub-lists is equal to the total number of groups of data partitioned by
Author: Predictive Analytics Team at Pivotal Inc.
Maintainer: Frank McQuillan, Pivotal Inc. [email protected]
Aggregate functions lists all the supported aggregate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
## Not run: ## help("by,db.obj-method") # display this doc ## set up the database connection ## Assume that .port is port number and .dbname is the database name cid <- db.connect(port = .port, dbname = .dbname, verbose = FALSE) ## create a table from the example data.frame "abalone" x <- as.db.data.frame(abalone, conn.id = cid, verbose = FALSE) ## mean values for each column lk(by(x, x$sex, mean)) ## No need to compute the mean of id and sex lk(by(x[,-c(1,2)], x$sex, mean)) lk(by(x[,-c(1,2)], x[,2], mean)) # the same lk(by(x[,-c(1,2)], x[,"sex"], mean)) # the same ## The return type of FUN is not db.obj dat <- x ## Fit linear model to each group of data by(dat, dat$sex, function(x) madlib.lm(rings ~ . - id - sex, data = x)) db.disconnect(cid, verbose = FALSE) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.