R重塑矩阵并连接列

时间:2014-05-22 16:12:04

标签: r dataframe reshape

在提问之前我尝试使用cast,dcast,但一切都出错了......无论如何,我有一个像这样的data.frame

Genes                                               Code
 Apod Tptm      cell attachment cell to cell contact cell polarity
 Apod Serpine1  cell attachment cell to cell contact cell polarity
 Adm Ramp2                                        angiogenesis
 Adm Rdr                                          angiogenesis

我的想法是使用代码列对data.frame进行整形,以便在每种情况下将唯一代码作为行和唯一的基因(避免重复)共同获取

Code                                                  Genes
cell attachment cell to cell contact cell polarity    Apod TpTm Serpine1
angiogenesis                                          Adm Rpr Ramp2

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

我认为这也会在基础R中实现。

#sample data
test <- data.frame(Genes = c("Apod Tptm", "Apod Serpine1", "Adm Ramp2", "Adm Rdr"),
                   Code = c("X", "X", "Y", "Y"))

> test
#          Genes Code
#1     Apod Tptm    X
#2 Apod Serpine1    X
#3     Adm Ramp2    Y
#4       Adm Rdr    Y


test <- aggregate(Genes ~ Code, data=test, function(x) 
          paste(unique(unlist(strsplit(paste(x, sep=" "), " "))), collapse =" "))  

#result
> test
#  Code              Genes
#1    X Apod Tptm Serpine1
#2    Y      Adm Ramp2 Rdr