Skip to content

Commit

Permalink
Rename count to tally to avoid conflict with plyr
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Nov 20, 2013
1 parent e293ecf commit 789b561
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ export(collect)
export(compare_tbls)
export(compute)
export(copy_to)
export(count)
export(desc)
export(dim_desc)
export(do)
Expand Down Expand Up @@ -337,6 +336,7 @@ export(src_sql)
export(src_sqlite)
export(src_tbls)
export(summarise)
export(tally)
export(tbl)
export(tbl_cpp)
export(tbl_cube)
Expand Down
32 changes: 16 additions & 16 deletions R/count.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#' Count observations by group.
#' Tally observations by group.
#'
#' \code{count} is a convenient wrapper for summarise that either call
#' \code{\link{n}} or \code{\link{sum}(n)} depending on whether you're counting
#' for the first time, or recounting.
#' \code{tally} is a convenient wrapper for summarise that either call
#' \code{\link{n}} or \code{\link{sum}(n)} depending on whether you're tallying
#' for the first time, or re-tallying.
#'
#' @param a \code{\link{tbl}} to count
#' @param wt if not specified, will count the number of rows. If specified,
#' will perform a "weighted" count but summing over the specified variable.
#' @param a \code{\link{tbl}} to tally
#' @param wt if not specified, will tally the number of rows. If specified,
#' will perform a "weighted" tally but summing over the specified variable.
#' @export
#' @examples
#' if (require("Lahman")) {
#' batting_tbl <- tbl_cpp(Batting)
#' count(group_by(batting_tbl, yearID))
#' tally(group_by(batting_tbl, yearID))
#'
#' plays_by_year <- count(group_by(batting_tbl, playerID, stint))
#' count(plays_by_year)
#' count(count(plays_by_year))
#' count(group_by(plays_by_year, stint))
#' plays_by_year <- tally(group_by(batting_tbl, playerID, stint))
#' tally(plays_by_year)
#' tally(tally(plays_by_year))
#' tally(group_by(plays_by_year, stint))
#'
#' # This looks a little nicer if you use the infix %.% operator
#' batting_tbl %.% group_by(playerID) %.% count()
#' batting_tbl %.% group_by(playerID) %.% tally()
#' }
count <- function(x, wt) {
tally <- function(x, wt) {
if (missing(wt)) {
if ("n" %in% names(x)) {
message("Using n as weighting variable")
Expand Down Expand Up @@ -57,11 +57,11 @@ count <- function(x, wt) {
#' @examples
#' if (require("Lahman")) {
#' players <- group_by(tbl_df(Batting), playerID)
#' games <- count(players, G)
#' games <- tally(players, G)
#' top_n(games, 10, n)
#'
#' # A little nicer with %.%
#' tbl_df(Batting) %.% group_by(playerID) %.% count(G) %.% top_n(10)
#' tbl_df(Batting) %.% group_by(playerID) %.% tally(G) %.% top_n(10)
#' }
top_n <- function(x, n, wt = NULL) {
if (is.null(wt)) {
Expand Down
34 changes: 34 additions & 0 deletions man/tally.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
\name{tally}
\alias{tally}
\title{Tally observations by group.}
\usage{
tally(x, wt)
}
\arguments{
\item{a}{\code{\link{tbl}} to tally}

\item{wt}{if not specified, will tally the number of
rows. If specified, will perform a "weighted" tally but
summing over the specified variable.}
}
\description{
\code{tally} is a convenient wrapper for summarise that
either call \code{\link{n}} or \code{\link{sum}(n)}
depending on whether you're tallying for the first time, or
re-tallying.
}
\examples{
if (require("Lahman")) {
batting_tbl <- tbl_cpp(Batting)
tally(group_by(batting_tbl, yearID))
plays_by_year <- tally(group_by(batting_tbl, playerID, stint))
tally(plays_by_year)
tally(tally(plays_by_year))
tally(group_by(plays_by_year, stint))
# This looks a little nicer if you use the infix \%.\% operator
batting_tbl \%.\% group_by(playerID) \%.\% tally()
}
}
4 changes: 2 additions & 2 deletions man/top_n.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ group, ordered by \code{wt}.
\examples{
if (require("Lahman")) {
players <- group_by(tbl_df(Batting), playerID)
games <- count(players, G)
games <- tally(players, G)
top_n(games, 10, n)

# A little nicer with \%.\%
tbl_df(Batting) \%.\% group_by(playerID) \%.\% count(G) \%.\% top_n(10)
tbl_df(Batting) \%.\% group_by(playerID) \%.\% tally(G) \%.\% top_n(10)
}
}

0 comments on commit 789b561

Please sign in to comment.