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

Time:05-06

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

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

CodePudding user response:

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:

sapply(iris, function(x) length(unique(x)))

Output

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

CodePudding user response:

DataframeAldex2 <- apply(DataframeAldex, 1, function(x) paste0(x))
dim(unique(DataframeAldex2))
  • Related