R中数据帧中每一行的唯一元素计数 [英] Count of unique elements of each row in a data frame in R
本文介绍了R中数据帧中每一行的唯一元素计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下数据框:
Group1 Group2 Group3 Group4
A B A B
A C B A
B B B B
A C B D
A D C A
我想添加一个新列到数据帧,该数据帧将在每一行中包含唯一元素的计数。所需的输出:
I want to add a new column to the data frame which will have the count of unique elements in each row. Desired output:
Group1 Group2 Group3 Group4 Count
A B A B 2
A C B A 3
B B B B 1
A C B D 4
A D C A 3
我能够找到这样的计数
length(unique(c(df[,c(1,2,3,4)][1,])))
我想对数据框中的所有行执行相同的操作。我尝试使用var = 1的apply(),但没有成功。另外,如果您可以为此提供更优雅的解决方案,那就太好了。
I want to do the same thing for all rows in the data frame. I tried apply() with var=1 but without success. Also, it would be great if you could provide a more elegant solution to this.
推荐答案
我们可以使用 apply
和 MARGIN = 1
遍历行
df1$Count <- apply(df1, 1, function(x) length(unique(x)))
df1$Count
#[1] 2 3 1 4 3
或使用 tidyverse
library(dplyr)
df1 %>%
rowwise() %>%
do(data.frame(., Count = n_distinct(unlist(.))))
# A tibble: 5 × 5
# Group1 Group2 Group3 Group4 Count
#* <chr> <chr> <chr> <chr> <int>
#1 A B A B 2
#2 A C B A 3
#3 B B B B 1
#4 A C B D 4
#5 A D C A 3
我们也可以使用 regex
这以更快的方式。它基于这样的假设:每个单元格中只有一个字符
We can also use regex
to do this in a faster way. It is based on the assumption that there is only a single character per each cell
nchar(gsub("(.)(?=.*?\\1)", "", do.call(paste0, df1), perl = TRUE))
#[1] 2 3 1 4 3
给出了更详细的解释此处
这篇关于R中数据帧中每一行的唯一元素计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文