'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
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 1:[1]
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
Solution 2:[2]
DataframeAldex2 <- apply(DataframeAldex, 1, function(x) paste0(x))
dim(unique(DataframeAldex2))
Solution 3:[3]
We may use fndistinct
from collapse
library(collapse)
fndistinct(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
35 23 43 22 3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Aidan |
Solution 3 | akrun |