Home > Mobile >  Count the number of observations in the data frame in R
Count the number of observations in the data frame in R

Time:07-10

I want to know the way of counting the number of observations using R. For example, let's say I have a data df as follows:

df <- data.frame(id = c(1,1,1,2,2,2,2,3,3,5,5,5,9,9)) Even though the biggest number of id is 9, there are only 5 numbers: 1,2,3,5,and 9. So there are only 5 numbers in id. I want to count how many numbers exist in id like this.

CodePudding user response:

Here another two options:

df <- data.frame(id = c(1,1,1,2,2,2,2,3,3,5,5,5,9,9))

sum(!duplicated(df$id))
#> [1] 5
library(dplyr) 
n_distinct(df$id)
#> [1] 5

Created on 2022-07-09 by the reprex package (v2.0.1)

CodePudding user response:

In base R:

length(unique(df$id))
[1] 5

Here, unique filters only distinct values and length then counts the number of values in the vector

In dplyr:

df %>%
  summarise(n = length(unique(id)))

Alternatively:

nrow(distinct(df))

Here, distinct subsets the whole dataframe (not just the column id!) to unique rows before nrow counts the number of remaining rows

  • Related