OREdplyr
functions for aggregating columns and rows.
Table 3-5 Aggregating Columns and Rows
Function | Description |
---|---|
|
Counts rows by group; similar to |
|
Summarizes columns by using aggregate functions. When an |
tally |
Tallies rows by group; a convenient wrapper for |
Example 3-74 Aggregating Columns
The following examples use the ore.frame
object MTCARS that is created by using the ore.push
function on the mtcars data.frame
object. They exemplify the use of the aggregation functions count
, summarize
, and tally
. They also use the OREdplyr
functions arrange
and group_by
.
MTCARS <- ore.push(mtcars) arrange(tally(group_by(MTCARS, cyl)), cyl) tally(group_by(MTCARS, cyl), sort = TRUE) # Multiple tallys progressively roll up the groups cyl_by_gear <- tally(group_by(MTCARS, cyl, gear), sort = TRUE) tally(cyl_by_gear, sort = TRUE) tally(tally(cyl_by_gear)) cyl_by_gear <- tally(group_by(MTCARS, cyl, gear), wt = hp, sort = TRUE) tally(cyl_by_gear, sort = TRUE) tally(tally(cyl_by_gear)) cyl_by_gear <- count(MTCARS, cyl, gear, wt = hp + mpg, sort = TRUE) tally(cyl_by_gear, sort = TRUE) tally(tally(cyl_by_gear)) # Load the magrittr library to use the forward-pipe operator %>% library(magrittr) MTCARS %>% group_by(cyl) %>% tally(sort = TRUE) # count is more succinct and also does the grouping MTCARS %>% count(cyl) %>% arrange(cyl) MTCARS %>% count(cyl, wt = hp) %>% arrange(cyl) MTCARS %>% count_("cyl", wt = hp, sort = TRUE)
Listing for This Example
R> MTCARS <- ore.push(mtcars) R> arrange(tally(group_by(MTCARS, cyl)), cyl) cyl n 1 4 11 2 6 7 3 8 14 R> tally(group_by(MTCARS, cyl), sort = TRUE) cyl n 1 8 14 2 4 11 3 6 7 R> R> # Multiple tallys progressively roll up the groups R> cyl_by_gear <- tally(group_by(MTCARS, cyl, gear), sort = TRUE) R> tally(cyl_by_gear, sort = TRUE) Using n as weighting variable cyl n 1 8 14 2 4 11 3 6 7 R> tally(tally(cyl_by_gear)) Using n as weighting variable Using n as weighting variable n 32 R> R> cyl_by_gear <- tally(group_by(MTCARS, cyl, gear), wt = hp, sort = TRUE) R> tally(cyl_by_gear, sort = TRUE) Using n as weighting variable cyl n 1 8 2929 2 4 909 3 6 856 R> tally(tally(cyl_by_gear)) Using n as weighting variable Using n as weighting variable n 4694 R> R> cyl_by_gear <- count(MTCARS, cyl, gear, wt = hp + mpg, sort = TRUE) R> tally(cyl_by_gear, sort = TRUE) Using n as weighting variable cyl n 1 8 3140.4 2 4 1202.3 3 6 994.2 R> tally(tally(cyl_by_gear)) Using n as weighting variable Using n as weighting variable n 5336.9 R> R> # Load the magrittr library to use the forward-pipe operator %>% R> library(magrittr) R> MTCARS %>% group_by(cyl) %>% tally(sort = TRUE) cyl n 1 8 14 2 4 11 3 6 7 R> R> # count is more succinct and also does the grouping R> MTCARS %>% count(cyl) %>% arrange(cyl) cyl n 1 4 11 2 6 7 3 8 14 R> MTCARS %>% count(cyl, wt = hp) %>% arrange(cyl) cyl n 1 4 909 2 6 856 3 8 2929 R> MTCARS %>% count_("cyl", wt = hp, sort = TRUE) cyl n 1 8 2929 2 4 909 3 6 856