How to extract unique elements from data.frame in R?
Can anyone suggest a way to extract unique elements from a data.frame?
I noticed unique
, but since it works on any rows or columns, it doesn't do what I'm looking for. I am behind unique cells from data.frame.
eg.
df<-data.frame(V1=c("Hello","fat","man"),V2=c("cat","fat","Hello"),V3=c("man","dog","black"))
Extracting unique items should give me c("Hello","fat","man","cat","dog","black")
A 'data.frame' can be thought of as a "list" with columns as "list" items having the same length. Using unlist
, we can convert it to vector
and then get the values unique
and convert to the 'character' class with as.character
.
as.character(unique(unlist(df)))