使用rbind合并数据帧

时间:2013-11-18 14:28:26

标签: r

我有2个数据框A和B,尺寸为2 x 5,如下所示:

 A = data.frame(GeneA1=-0.02:1.89, GeneB2=0.25:1.99, GeneB3=0.17:1.87, GeneB4=0.3:1.63, GeneC2=0.29:1.97, row.names=c("sample 1", "sample 2"))

 B = data.frame(GeneA1=0.52:-0.04, GeneB1=1.1:0.08, GeneB3=0.72:0.03, GeneB5=0.78:0.06, GeneC2=0.78:0.25, row.names=c("sample 1", "sample 2"))    

对于A& B,行是样本,列是基因类型

我想尝试合并A& B使用rbind,添加基因类型不匹配的NA s。我听说有一种方法可以使用setdiff参数,但我不知道怎么做?

3 个答案:

答案 0 :(得分:3)

使用merge

> AB <- merge(A, B, all=TRUE)
> AB[,order(names(AB))]  # to get the result ordered by colnames 
  Gene A1 Gene B1 Gene B2 Gene B3 Gene B4 Gene B5 Gene C2
1   -0.04    0.08      NA    0.03      NA    0.06    0.25
2   -0.02      NA    0.25    0.17    0.30      NA    0.29
3    0.52    1.10      NA    0.72      NA    0.78    0.78
4    1.89      NA    1.99    1.87    1.63      NA    1.97

AB的位置如下:

A <- matrix(c(-0.02, 0.25, 0.17, 0.3, 0.29, 
              1.89, 1.99, 1.87, 1.63, 1.97), 
            nrow=2, byrow=TRUE,
            dimnames=list(NULL, c("Gene A1", "Gene B2", 
                                  "Gene B3", 
                                  "Gene B4", "Gene C2")))

B <- matrix(c(0.52, 1.1, 0.72, 0.78, 0.78, 
              -0.04, 0.08, 0.03, 0.06,0.25), 
            nrow=2, byrow=TRUE,
            dimnames=list(NULL, c("Gene A1", "Gene B1",
                                  "Gene B3", 
                                  "Gene B5", "Gene C2")))

答案 1 :(得分:1)

您可以使用merge

功能
A=data.frame(A1=c(-0.02,1.89),B2=c(0.25,1.99),B3=c(0.17,1.87),B4=c(0.3,1.63),C2=c(0.29,1.97))
B=data.frame(A1=c(0.52,-0.04),B1=c(1.1,0.08),B3=c(0.72,0.03),B5=c(0.78,0.06),C2=c(0.78,0.25))
C<-merge(A, B, all=T)
View(C)

答案 2 :(得分:0)

试试这个:

# dummy data
A <- read.table(text="
Gene A1, Gene B2, Gene B3, Gene B4, Gene C2
0.52, 0.25, 0.17, 0.3, 0.29
1.89, 1.99, 1.87, 1.63, 1.97",
                sep=",", header=TRUE)
B <- read.table(text="
Gene A1, Gene B1, Gene B3, Gene B5, Gene C2
0.52, 1.1, 0.72, 0.78, 0.78
-0.04, 0.08, 0.03, 0.06,0.25",
                sep=",", header=TRUE)

#transpose and merge
tAB <- merge(t(A),t(B),by="row.names",all=TRUE)

#keep gene names
col <- tAB[,1]

#exclude rownames, transpose
output <- t(tAB[,-1])

#update colnames
colnames(output) <- col

#output
#     Gene.A1 Gene.B1 Gene.B2 Gene.B3 Gene.B4 Gene.B5 Gene.C2
#V1.x   -0.02      NA    0.25    0.17    0.30      NA    0.29
#V2.x    1.89      NA    1.99    1.87    1.63      NA    1.97
#V1.y    0.52    1.10      NA    0.72      NA    0.78    0.78
#V2.y   -0.04    0.08      NA    0.03      NA    0.06    0.25