tally: Counts/tally observations by group.

Description Usage Arguments Examples

View source: R/tally.R

Description

tally is a convenient wrapper for summarise that will either call n or sum(n) depending on whether you're tallying for the first time, or re-tallying. count() is similar, but also does the group_by for you.

Usage

1
2
3
4
5
tally(x, wt, sort = FALSE)

count(x, ..., wt = NULL, sort = FALSE)

count_(x, vars, wt = NULL, sort = FALSE)

Arguments

x

a tbl to tally/count.

wt

(Optional) If omitted, will count the number of rows. If specified, will perform a "weighted" tally by summing the (non-missing) values of variable wt.

sort

if TRUE will sort output in descending order of n

..., vars

Variables to group by.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
if (require("Lahman")) {
batting_tbl <- tbl_df(Batting)
tally(group_by(batting_tbl, yearID))
tally(group_by(batting_tbl, yearID), sort = TRUE)

# Multiple tallys progressively roll up the groups
plays_by_year <- tally(group_by(batting_tbl, playerID, stint), sort = TRUE)
tally(plays_by_year, sort = TRUE)
tally(tally(plays_by_year))

# This looks a little nicer if you use the infix %>% operator
batting_tbl %>% group_by(playerID) %>% tally(sort = TRUE)

# count is even more succinct - it also does the grouping for you
batting_tbl %>% count(playerID)
batting_tbl %>% count(playerID, wt = G)
batting_tbl %>% count(playerID, wt = G, sort = TRUE)
}

sctyner/dplyr050 documentation built on May 17, 2019, 2:22 p.m.