如何计算 R 中跨多个列的列中的唯一条目
How to count unique entries in a column across multiple columns in R
我有一个看起来像这样的数据集
data set example
我试图在每一列中找到唯一的条目
我设法利用 count
完成了 1 列
ORDDF4ALDEX_Count <- DataframeAldex %>% count(Zotu63864)
但是,我想知道是否有一种方法可以在不通过计数输入每个列名的情况下对所有列执行此操作
谢谢
如果您想计算每一列中有多少个唯一数字,那么我们可以在 tidyverse
.
中的 summarise
中使用 n_distinct
library(tidyverse)
iris %>%
summarise(across(everything(), ~ n_distinct(.)))
或以 R 为基数:
sapply(iris, function(x) length(unique(x)))
输出
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 35 23 43 22 3
DataframeAldex2 <- apply(DataframeAldex, 1, function(x) paste0(x))
dim(unique(DataframeAldex2))
我们可以使用 fndistinct
来自 collapse
library(collapse)
fndistinct(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
35 23 43 22 3
我有一个看起来像这样的数据集 data set example
我试图在每一列中找到唯一的条目
我设法利用 count
完成了 1 列
ORDDF4ALDEX_Count <- DataframeAldex %>% count(Zotu63864)
但是,我想知道是否有一种方法可以在不通过计数输入每个列名的情况下对所有列执行此操作
谢谢
如果您想计算每一列中有多少个唯一数字,那么我们可以在 tidyverse
.
summarise
中使用 n_distinct
library(tidyverse)
iris %>%
summarise(across(everything(), ~ n_distinct(.)))
或以 R 为基数:
sapply(iris, function(x) length(unique(x)))
输出
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 35 23 43 22 3
DataframeAldex2 <- apply(DataframeAldex, 1, function(x) paste0(x))
dim(unique(DataframeAldex2))
我们可以使用 fndistinct
来自 collapse
library(collapse)
fndistinct(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
35 23 43 22 3