1

我有这个数据框结构

数据1:

  SNP   logp    Allele
rs2929  rs333003    4.46411719154375    T
rs3491  rs405831    4.46411719154375    G
rs1224  rs179639    4.44797917307381    A   

和 Data2 想象它是一样的(也只是为了测试 Data1)

当我做 :

f1=read.table(data1, header=TRUE ,as.is=TRUE)
f3=rbind(f1,f1)

我得到这个结果??:

  SNP   logp    Allele
rs2929  rs333003    4.46411719154375    T
rs3491  rs405831    4.46411719154375    G
rs1224  rs179639    4.44797917307381    A
rs29291 rs333003    4.46411719154375    T
rs34911 rs405831    4.46411719154375    G
rs12241 rs179639    4.44797917307381    A

如您所见,问题是 rs2929 应该被复制两次,而是 rs29291 注意字符串中的所有重复项都是 +"1"?那是错的!?我怎么能把它变成:

  SNP   logp    Allele
rs2929  rs333003    4.46411719154375    T
rs3491  rs405831    4.46411719154375    G
rs1224  rs179639    4.44797917307381    A
rs2929  rs333003    4.46411719154375    T
rs3491  rs405831    4.46411719154375    G
rs1224  rs179639    4.44797917307381    A

因为那是真正的“合并”,我想加入 2 个文件。我知道这可能听起来很奇怪,因为我重复了值,但如果发生这种情况,这就是我想要的。

4

4 回答 4

2

来自?read.table

行名称 [...]

      If there is a header and the first row contains one fewer
      field than the number of columns, the first column in the
      input is used for the row names.  Otherwise if ‘row.names’ is
      missing, the rows are numbered.

因此,在您的情况下,第一列用作行名,并且由于 R 要求行名始终是唯一的,因此1在您尝试创建重复项时会附加这些行名。

一种解决方案是让您在文件的左上角单元格中添加列名,或使用以下命令调用 read.table row.names = NULL

f1 <- read.table(textConnection("

             SNP                logp    Allele
rs2929  rs333003    4.46411719154375         T
rs3491  rs405831    4.46411719154375         G
rs1224  rs179639    4.44797917307381         A   

"), header = TRUE, as.is = TRUE, row.names = NULL)

rbind(f1, f1)
#   row.names      SNP     logp Allele
# 1    rs2929 rs333003 4.464117      T
# 2    rs3491 rs405831 4.464117      G
# 3    rs1224 rs179639 4.447979      A
# 4    rs2929 rs333003 4.464117      T
# 5    rs3491 rs405831 4.464117      G
# 6    rs1224 rs179639 4.447979      A
于 2012-11-19T01:22:04.587 回答
0

您最左边的字符串(“rs2929”等)是行名。R 出于充分的理由希望对象中的名称是明确的,因此它正在重新分配一些名称。最简单的解决方案可能是不将此信息存储在行名中,而是存储在普通列中。

f1$rs <- rownames(f1)
rownames(f1) <- NULL
f1new <- rbind(f1,f1)
于 2012-11-19T01:23:22.233 回答
0

您在第一列上缺少列标签意味着它们被解释为行标签,而不是数据。

看到这个:

 f1 <- structure(list(X = c("rs2929", "rs3491", "rs1224"), SNP = c("rs333003", 
   "rs405831", "rs179639"), logp = c(4.46411719154375, 4.46411719154375, 
   4.44797917307381), Allele = c("T", "G", "A")), .Names = c("X", 
   "SNP", "logp", "Allele"), class = "data.frame", row.names = c(NA, -3L))

 rbind(f1,f1)

R 喜欢独特的行标签。

于 2012-11-19T01:24:28.047 回答
0

所以我想出了我需要的这个功能:

merge_gwas("final.txt","file1.txt","file2.txt")

merge_gwas<-function (out,a1,a2){
f1=read.table(a1,header=T,as.is=TRUE)
f2=read.table(a2,header=T,as.is=TRUE)
f3<- rbind(f1,f2)
write.table(f3, out,quote = FALSE, sep="\t", row.names = FALSE)
}

感谢大家

于 2012-11-19T22:44:34.343 回答