0

假设我有一个如下所示的 data.frame:

structure(list(HUGO.Int1 = c("AATF", "AATF", "AATF", "ABHD16A", 
"ABHD16A", "ABHD16A"), Mut.Int1 = c(0, 0, 0, 0, 0, 0), HUGO.Int2 = c("DAPK3", 
"PAWR", "CHEK1", "RNF5", "ATP5G3", "HM13"), Mut.Int2 = c(0, 0, 
0, 0, 0, 0)), .Names = c("HUGO.Int1", "Mut.Int1", "HUGO.Int2", 
"Mut.Int2"), row.names = c(12223L, 15033L, 31655L, 11280L, 11463L, 
14106L), class = "data.frame")

我想要HUGO.Int2并且Mut.Int2下面 HUGO.Int1并且Mut.Int1

我试过了

data.frame(rbind(c(plots$HUGO.Int1, plots$Mut.Int1), 
           c(plots$HUGO.Int2, plots$Mut.Int2)))

但后来我只得到 2 列,Hugo.int1并且Mut.int1. 我想知道如何正确地将第 3 列和第 4 列粘贴到第 1 列和第 2 列下方。

4

4 回答 4

2

问题是你卡rbind在那里,有点随意。这就是您的尝试可能应该看起来的样子:

dat <- structure(list(HUGO.Int1 = c("AATF", "AATF", "AATF", "ABHD16A", 
+ "ABHD16A", "ABHD16A"), Mut.Int1 = c(0, 0, 0, 0, 0, 0), HUGO.Int2 = c("DAPK3", 
+ "PAWR", "CHEK1", "RNF5", "ATP5G3", "HM13"), Mut.Int2 = c(0, 0, 
+ 0, 0, 0, 0)), .Names = c("HUGO.Int1", "Mut.Int1", "HUGO.Int2", 
+ "Mut.Int2"), row.names = c(12223L, 15033L, 31655L, 11280L, 11463L, 
+ 14106L), class = "data.frame")
> data.frame(col1 = c(dat[,1],dat[,3]),col2 = c(dat[,2],dat[,4]))
      col1 col2
1     AATF    0
2     AATF    0
3     AATF    0
4  ABHD16A    0
5  ABHD16A    0
6  ABHD16A    0
7    DAPK3    0
8     PAWR    0
9    CHEK1    0
10    RNF5    0
11  ATP5G3    0
12    HM13    0
于 2013-01-18T19:17:33.600 回答
2

其他方式:

names(x) <- chartr('12', 'XX', names(x)) # replaces 1s and 2s in names with 'X'
rbind(x[1:2], x[3:4])
于 2013-01-18T19:27:56.383 回答
1

不确定这是否是您想要的,但它有效:

data1 <- structure(list(HUGO.Int1 = c("AATF", "AATF", "AATF", "ABHD16A", 
"ABHD16A", "ABHD16A"), Mut.Int1 = c(0, 0, 0, 0, 0, 0), HUGO.Int2 = c("DAPK3", 
"PAWR", "CHEK1", "RNF5", "ATP5G3", "HM13"), Mut.Int2 = c(0, 0, 
0, 0, 0, 0)), .Names = c("HUGO.Int1", "Mut.Int1", "HUGO.Int2", 
"Mut.Int2"), row.names = c(12223L, 15033L, 31655L, 11280L, 11463L, 
14106L), class = "data.frame")

data2 <- cbind(data1$HUGO.Int2, data1$Mut.Int2)
data1$HUGO.Int2 <- NULL
data1$Mut.Int2 <- NULL
colnames(data2) <- colnames(data1)
data3 <- rbind(data1,data2)


> data3 
      HUGO.Int1 Mut.Int1
12223      AATF        0
15033      AATF        0
31655      AATF        0
11280   ABHD16A        0
11463   ABHD16A        0
14106   ABHD16A        0
7         DAPK3        0
8          PAWR        0
9         CHEK1        0
10         RNF5        0
11       ATP5G3        0
12         HM13        0
于 2013-01-18T19:16:37.100 回答
1
df <- structure(list(HUGO.Int1 = c("AATF", "AATF", "AATF", "ABHD16A", 
"ABHD16A", "ABHD16A"), Mut.Int1 = c(0, 0, 0, 0, 0, 0), HUGO.Int2 = c("DAPK3", 
"PAWR", "CHEK1", "RNF5", "ATP5G3", "HM13"), Mut.Int2 = c(0, 0, 
0, 0, 0, 0)), .Names = c("HUGO.Int1", "Mut.Int1", "HUGO.Int2", 
"Mut.Int2"), row.names = c(12223L, 15033L, 31655L, 11280L, 11463L, 
14106L), class = "data.frame")

df2 <- df[,1:2]
df2[7:12,1:2] <- df[,3:4]
于 2013-01-18T19:21:27.457 回答