如何从某列添加行开头?

时间:2015-12-15 11:42:53

标签: r rbind

我有一个数据,例如

main.ts

对于每个SNP,我计算了矩阵

中的某个值
ID Phenotype SNP1 SNP2 SNP3 .....SNP100
 1  1         0    2    1          0
 2  1         0    0    1          2

所以现在,我想在上面的原始数据中插入这些值,以便我可以稍后对数据(排名)进行排序。

           [,1]
 SNP1      0.1
 SNP2      0.5
 SNP3      0.2
  :
 SNP100    0.3

1 个答案:

答案 0 :(得分:1)

我们可以rbind

 rbind(data.frame(ID=NA, Phenotype=NA, t(m1[,1])), df1)
 #   ID Phenotype SNP1 SNP2 SNP3
 #1 NA        NA  0.1  0.5  0.2
 #2  1         1  0.0  2.0  1.0
 #3  2         1  0.0  0.0  1.0

数据

 df1 <- structure(list(ID = 1:2, Phenotype = c(1L, 1L), 
 SNP1 = c(0L, 
 0L), SNP2 = c(2L, 0L), SNP3 = c(1L, 1L)), .Names = c("ID", 
 "Phenotype", 
 "SNP1", "SNP2", "SNP3"), class = "data.frame",
 row.names = c(NA, -2L))

 m1 <- structure(c(0.1, 0.5, 0.2), .Dim = c(3L, 1L), 
 .Dimnames = list(c("SNP1", "SNP2", "SNP3"), NULL))