Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

count

Count the number of occurences.


Description

Equivalent to as.data.frame(table(x)), but does not include combinations with zero counts.

Usage

count(df, vars = NULL, wt_var = NULL)

Arguments

df

data frame to be processed

vars

variables to count unique values of

wt_var

optional variable to weight by - if this is non-NULL, count will sum up the value of this variable for each combination of id variables.

Details

Speed-wise count is competitive with table for single variables, but it really comes into its own when summarising multiple dimensions because it only counts combinations that actually occur in the data.

Compared to table + as.data.frame, count also preserves the type of the identifier variables, instead of converting them to characters/factors.

Value

a data frame with label and freq columns

See Also

table for related functionality in the base package

Examples

# Count of each value of "id" in the first 100 cases
count(baseball[1:100,], vars = "id")
# Count of ids, weighted by their "g" loading
count(baseball[1:100,], vars = "id", wt_var = "g")
count(baseball, "id", "ab")
count(baseball, "lg")
# How many stints do players do?
count(baseball, "stint")
# Count of times each player appeared in each of the years they played
count(baseball[1:100,], c("id", "year"))
# Count of counts
count(count(baseball[1:100,], c("id", "year")), "id", "freq")
count(count(baseball, c("id", "year")), "freq")

plyr

Tools for Splitting, Applying and Combining Data

v1.8.6
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.