如何从R中的data.frame中提取唯一的元素? [英] How to extract unique elements from a data.frame in R?

查看:1270
本文介绍了如何从R中的data.frame中提取唯一的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提出一种从data.frame中提取唯一元素的方法?



我注意到 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆