Learn R Programming

plyr (version 1.5.2)

count: Count the number of occurences.

Description

Count the number of occurences.

Usage

count(df, vars, wt_var)

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.

Value

  • a data frame with label and freq columns

Details

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

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.

Examples

Run this code
count(baseball, "id")
count(baseball, "id", "g")
count(baseball, "id", "ab")
count(baseball, "lg")
count(baseball, "stint")
count(count(baseball, c("id", "year")), "id", "freq")
count(count(baseball, c("id", "year")), "freq")

Run the code above in your browser using DataLab