Skip to main content
v2026.11,986 entries · CC-BY 4.0

Count Function in R: dplyr count(), wt, sort and table()

How count() in R tallies rows by group: dplyr arguments, weighted counts, zero-count factor levels, and how it differs from tally(), add_count() and table().

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

The count function in R usually means count() from the dplyr package: it returns how many rows of a data frame fall into each group of one or more variables, as a new data frame with a column named n. This page is for researchers who need frequency tables from tidy data and want to know when to use count(), tally(), add_count() or base R table(). Syntax and behaviour below follow the dplyr reference page and the base R manual, both read on 2026-11-08.

What count() is used for in research

Typical uses are tabulating participants per study arm, observations per site, or records per category before modelling. count() is a wrapper that groups transiently, so you get a summary table without leaving a grouped data frame behind. For grouping more generally see grouping in R; for summing a column see summation in R.

Count in R: three meanings

  • Rows per group: count(df, group) (dplyr).
  • Total rows in one group variable’s levels, as a named array: table(df$group) (base R).
  • Summed weight per group: count(df, group, wt = weight_column).

Install or access

count() ships in dplyr, which is on CRAN. Install and load it once per session:

install.packages("dplyr")
library(dplyr)

table() is part of base R and needs no installation. Reference pages: dplyr count/tally reference and base R table().

Step-by-step: count rows, weight them, sort them

The examples reuse the small data frame from the dplyr documentation, where runs is a numeric column.

df <- tribble(
  ~name,    ~gender,  ~runs,
  "Max",    "male",   10,
  "Sandra", "female", 1,
  "Susan",  "female", 4
)
  1. Count rows per group. df |> count(gender) returns one row per value of gender.
  2. Count by several variables. List more variables after the data: count(sex, gender, sort = TRUE) is the form used in the reference with the starwars data.
  3. Weight the count. df |> count(gender, wt = runs) sums runs within each gender instead of counting rows.
  4. Sort. count(species, sort = TRUE) puts the largest groups at the top.
  5. Count a computed value. Expressions work inside the call, for example count(birth_decade = round(birth_year, -1)).
  6. Rename the result column. name = "participants" replaces the default n.

Arguments of count()

Usage: count(x, ..., wt = NULL, sort = FALSE, name = NULL); the data frame method adds .drop = group_by_drop_default(x).

Argument What it does
x A data frame, tibble or lazy data frame (for example from dbplyr or dtplyr).
... Variables to group by.
wt Frequency weights. NULL (default) counts rows; a variable gives sum(wt) per group.
sort If TRUE, the largest groups appear at the top.
name Name of the count column. Defaults to n; if that name is already taken, dplyr uses nn, then nnn, and so on.
.drop For factors, FALSE includes counts for unused (empty) levels.

Output and how to read it

The result is the same type as the input (a tibble in, a tibble out). For count(gender) on the example above the documentation shows:

# A tibble: 2 x 2
  gender     n
  <chr>  <int>
1 female     2
2 male       1

With wt = runs the same call returns female 5 and male 10, and n becomes <dbl> because it is a sum of a numeric column, not a row count. Read n as rows when wt is absent and as total weight when it is present.

Zero counts and missing values

With a factor type holding a, NA, a, c, a and levels a, b, c, the reference shows that count(type) returns a 3, c 1 and NA 1; level b is absent. count(type, .drop = FALSE) adds b 0. Use .drop = FALSE when an empty category must appear in a report. NA forms its own group.

count() vs tally() vs add_count() vs table()

Function Usage What it returns
count() count(x, ..., wt, sort, name, .drop) One row per group with a count column; groups transiently.
tally() tally(x, wt, sort, name) Counts rows of the data as given; the reference pairs it with group_by(species) |> tally() for grouped counts.
add_count() add_count(x, ..., wt, sort, name) Keeps every row and adds the group count as a new column.
add_tally() add_tally(x, wt, sort, name) Like add_count() for already-grouped data.
table() (base R) table(..., exclude, useNA = c("no","ifany","always"), dnn, deparse.level) A contingency table of class “table” (an integer array); useNA controls whether NA is counted.

To create a new column from counts afterwards, see mutate function in R; to filter rows first, see subset in R. Data loaded with read.table in R can be passed straight to count().

Troubleshooting

  • Could not find function “count”: run library(dplyr) first; the function is not in base R.
  • Column named nn or nnn: your data already has a column called n. Set name yourself.
  • Count column is numeric, not integer: you passed wt, so the column is a sum.
  • Expected category missing: it has no rows. Use .drop = FALSE for factor levels.
  • Counting a plain vector: count() takes a data frame; use table(x) for a vector.

Free alternatives

Option Notes
Base R table() Included with R; returns an integer array of class “table”.
dplyr tally() / add_count() Same package as count(), different output shape (see table above).

FAQ

What is the count function in R?

In dplyr, count() counts rows per group of the variables you name and returns a data frame with a count column n. Base R has no function called count(); its equivalent is table().

How do I count in R by group and sort the result?

Use count(df, group, sort = TRUE); sort = TRUE places the largest groups first.

What is the difference between count() and tally()?

count(x, ...) takes variables to group by; tally(x) counts rows of the data as given, so group first with group_by().

How do I count with weights?

Pass wt = column. The result is sum(column) per group rather than a number of rows.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Count Function in R: dplyr count(), wt, sort and table()

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →