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
>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(.))))