如何从R中的data.frame中提取唯一的元素? [英] How to extract unique elements from a data.frame in R?
问题描述
我注意到 unique
,但由于它适用于任何行或列,它不会做我正在寻找。我来自一个data.frame的独特单元格。
例如,
df< -data.frame(V1 = c(Hello,fat,man),V2 = c(cat,fat,Hello),V3 = c ,狗,黑))
提取独特元素应该给我 c(Hello,fat,man,cat,dog,black)
data.frame可以被视为具有长度相同的list元素的列表。通过使用 unlist
,我们可以将其转换为向量
,然后获取唯一
值,并转换为'code> as.character 的'character'类。
as.character(unique(unlist(df)))
Can someone suggest a way to extract unique elements from a data.frame?
I have noticed unique
, but since it works on either rows or columns, it doesn't do what I'm looking for. I'm after unique cells from a data.frame.
e.g.,
df<-data.frame(V1=c("Hello","fat","man"),V2=c("cat","fat","Hello"),V3=c("man","dog","black"))
Extracting unique elements should give me c("Hello","fat","man","cat","dog","black")
A 'data.frame' can be considered as a 'list' with columns as 'list' elements having the same length. By using unlist
, we can convert it to vector
and then get the unique
values and convert to 'character' class with as.character
.
as.character(unique(unlist(df)))
这篇关于如何从R中的data.frame中提取唯一的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!