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
)
- Count rows per group.
df |> count(gender)returns one row per value ofgender. - Count by several variables. List more variables after the data:
count(sex, gender, sort = TRUE)is the form used in the reference with thestarwarsdata. - Weight the count.
df |> count(gender, wt = runs)sumsrunswithin each gender instead of counting rows. - Sort.
count(species, sort = TRUE)puts the largest groups at the top. - Count a computed value. Expressions work inside the call, for example
count(birth_decade = round(birth_year, -1)). - Rename the result column.
name = "participants"replaces the defaultn.
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. Setnameyourself. - Count column is numeric, not integer: you passed
wt, so the column is a sum. - Expected category missing: it has no rows. Use
.drop = FALSEfor factor levels. - Counting a plain vector:
count()takes a data frame; usetable(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
- dplyr reference, count/tally: https://dplyr.tidyverse.org/reference/count.html (read 2026-11-08).
- R documentation, table(): https://stat.ethz.ch/R-manual/R-devel/library/base/html/table.html (read 2026-11-08).







