HOWTO · R

How to Count Number of Observations in R

Count every row, rows that meet conditions, or occurrences by group in an R data frame with base R and dplyr.

On this page

In an R data frame, an observation is a row. Use nrow() for the total number of observations, a logical condition with sum() for matching rows, or count() from dplyr for frequencies by group. The examples use a small data frame so you can adapt the column names to your own data.

Count all rows with nrow()

nrow(data) returns the number of rows in data. This is the direct choice when every row should be counted. The example has five rows, and nrow(students) supplies the value printed below.

students <- data.frame(
  name = c("Ada", "Ben", "Cam", "Dee", "Eli"),
  passed = c(TRUE, FALSE, TRUE, TRUE, FALSE),
  group = c("A", "A", "B", "B", "B")
)

total <- nrow(students)
cat("Total observations:", total, "\n")
Total observations: 5

For a total row count, nrow() is clearer than constructing a logical vector. In particular, with(data, 1) evaluates the scalar 1; it does not create one value for every row, so sum(with(data, 1)) is not a row-counting method.

Count rows that match conditions in base R

To count rows matching a condition, create a logical vector and pass it to sum(). In R, TRUE contributes 1 and FALSE contributes 0 when summed. You can use with() to refer to data-frame columns without repeating the data-frame name:

sum(with(students, passed))

For multiple conditions, combine them with & for “and” or | for “or.” For example, sum(with(students, passed & group == "B")) counts students who passed and belong to group B. Use parentheses when they make a combined condition easier to read.

If a condition contains NA, sum() returns NA unless you handle those values. Use na.rm = TRUE when unknown values should be excluded from the count, as in sum(students$passed, na.rm = TRUE). For an expression such as students$score > 70, this counts only known scores above 70; it does not treat missing scores as matches.

You can also subset the data frame and count its resulting rows with nrow():

nrow(students[students$passed, , drop = FALSE])

The drop = FALSE argument keeps the subset in data-frame form even when it contains one column. If the condition can contain NA, remove unknown rows from the logical index first, for example students[!is.na(students$passed) & students$passed, , drop = FALSE]; otherwise base R can include an all-NA row for an unknown condition. Use this approach when you need the filtered rows for later work; use sum() when you only need the count. For filtering followed by a row count in another example, see counting rows by group.

Count rows and groups with dplyr

dplyr provides filter() to select rows and count() to report how many rows occur in each group. These operations answer different questions: use filter() followed by nrow() for the number of rows that meet a condition, and use count() when you want a frequency for each value or combination of values.

For example, nrow(dplyr::filter(students, passed)) counts the students whose passed value is TRUE; filter() drops rows when its condition is NA. To count observations in each group, use dplyr::count(students, group). To count combinations, provide more grouping columns, as in dplyr::count(students, group, passed). These calls are illustrative and unexecuted here; the supplied runtime evidence covers only the total-row example.

count() returns the grouping columns and a count column named n by default. Its wt argument instead sums a weight column within each group, which is a weighted total rather than an ordinary number of rows. For a plain observation count, leave wt unset.

Choose the method that matches your question

  • Use nrow(data) to count every row.
  • Use sum(condition) or nrow(data[condition, , drop = FALSE]) to count rows selected by a condition.
  • Use filter() and nrow() when you want to count filtered rows with dplyr; use count() when you want frequencies by group.

These choices keep the meaning of the result explicit: total rows, rows meeting a condition, or a frequency table. Check that the columns used in a condition or grouping expression represent the values you intend to count.