且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R在一列中查找重复项并在第二列中折叠

更新时间:2022-11-14 13:14:34

可以在base R中使用tapply

You can use tapply in base R

data.frame(probes=unique(olap$probes), 
           genes=tapply(olap$genes, olap$probes, paste, collapse=" "))

或使用 plyr:

library(plyr)
ddply(olap, "probes", summarize, genes = paste(genes, collapse=" "))

更新

在第一个版本中这样做可能更安全:

It's probably safer in the first version to do this:

tmp <- tapply(olap$genes, olap$probes, paste, collapse=" ")
data.frame(probes=names(tmp), genes=tmp)

以防万一 unique 以与 tapply 不同的顺序提供探针.就我个人而言,我总是使用 ddply.

Just in case unique gives the probes in a different order to tapply. Personally I would always use ddply.