如何删除重复的字符串并合并所有列字符串?

时间:2016-09-16 21:40:49

标签: r

我的数据如下df

df<- structure(list(V1 = structure(c(5L, 1L, 2L, 3L, 4L), .Label = c("DNAJC11;FGOTG", 
"MAPK14", "PPIB", "RBX1", "USP14"), class = "factor"), V2 = structure(c(4L, 
3L, 2L, 1L, 1L), .Label = c("", "DNAJC9", "MAPK14", "USP14"), class = "factor"), 
    V3 = structure(c(3L, 2L, 4L, 5L, 1L), .Label = c("", "DNAJC11;FGOTG", 
    "GCLC", "GSR", "STIP1"), class = "factor")), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -5L))

我想将所有列合并为一个,然后保留唯一的列 例如,输出应该如下所示

USP14
DNAJC11;FGOTG
MAPK14
PPIB
RBX1
DNAJC9
GCLC
GSR
STIP1

我尝试使用melt功能,但我无法弄清楚如何执行此操作,任何评论都表示赞赏。谢谢

1 个答案:

答案 0 :(得分:1)

unique(as.vector(as.matrix(df)))

删除没有字符的条目:

vec<-unique(as.vector(as.matrix(df)))
vec[-which(vec=="")]

或者,礼貌@rawr

Filter(nzchar, unique(as.vector(as.matrix(df))))