forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtally.Rd
52 lines (43 loc) · 1.57 KB
/
tally.Rd
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tally.R
\name{tally}
\alias{count}
\alias{count_}
\alias{tally}
\title{Counts/tally observations by group.}
\usage{
tally(x, wt, sort = FALSE)
count(x, ..., wt = NULL, sort = FALSE)
count_(x, vars, wt = NULL, sort = FALSE)
}
\arguments{
\item{x}{a \code{\link{tbl}} to tally/count.}
\item{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 \code{wt}.}
\item{sort}{if \code{TRUE} will sort output in descending order of \code{n}}
\item{..., vars}{Variables to group by.}
}
\description{
\code{tally} is a convenient wrapper for summarise that will either call
\code{\link{n}} or \code{\link{sum}(n)} depending on whether you're tallying
for the first time, or re-tallying. \code{count()} is similar, but also
does the \code{\link{group_by}} for you.
}
\examples{
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)
}
}