列と行を集計するOREdplyr
関数。
表3-5 列と行の集計
関数 | 説明 |
---|---|
|
グループ別に行をカウントします。 |
|
集計関数を使用して、列をまとめます。 |
tally |
グループ別に行を集計します。最初の集計か再集計に応じて |
例3-74 列の集計
次の例では、mtcars data.frame
オブジェクトでore.push
関数を使用して作成されるore.frame
オブジェクトMTCARSを使用します。集計関数count
、summarize
およびtally
の使用例を示します。また、OREdplyr
関数arrange
および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)
この例のリスト
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