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

How to count unique entries in a column across multiple columns in R

I have a data set that looks something like this
data set example

I am trying to find unique entries in each of the columns

I managed to do it for 1 column utilizing count

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

ORDDF4ALDEX_Count <- DataframeAldex %>% count(Zotu63864)

however, I was wondering if there was a way to do so for all columns without entering each column name via count

Thanks

>Solution :

If you are wanting to count how many unique numbers there are in each column, then we can use n_distinct inside summarise from tidyverse.

library(tidyverse)

iris %>%
  summarise(across(everything(), ~ n_distinct(.)))

Or with base R:

apply(iris, 2, function(x) length(unique(x)))

Output

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           35          23           43          22       3

Or if you just want the count of non-NA values, then you could do this in base R:

colSums(!is.na(iris))

# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#        150          150          150          150          150

Or with tidyverse:

  iris %>%
    summarise(across(everything(), ~ sum(!is.na(.))))
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