Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count the number of observations in the data frame in R

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading