在 R 中保留矩阵的 top-left 列

Retaining the top-left column of matrices in R

我编写了一个通用脚本,用于以 cell-wise 方式平均 'stacks' 矩阵。我写出了平均文件,但在 matrices/tables 等

之间转换过程中,与行名称的列标题对应的单元格在某个时刻被丢弃

有没有办法让R 'respect'这个单元格(左上角),这样当我写出文件时它会一直存在?我需要为下游的另一个脚本保留它。

我只考虑 'injecting' 返回 write-time 的单元格,但这感觉很乱,如果我想将其推广,我必须向 argparse 添加一个参数。到目前为止,我只能发现 write.tableheader = T/F 选项,但这似乎并没有为左上角的列提供任何额外的内容。

代码如下:

# Standard install if missing
list.of.packages <- c("argparse", "abind")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
for(i in list.of.packages){suppressMessages(library(i, character.only = TRUE))}


# Parse commandline arguments
parser <- ArgumentParser()
parser$add_argument('-i',
                    '--infiles',
                    nargs='+',
                    required=TRUE,
                    help="All the matrices to average.")
parser$add_argument('-s',
                    '--separator',
                    action='store',
                    default='\t',
                    help='The field separator for the input matrices (they should all match). [Def = \t].')
parser$add_argument('-o',
                    '--outfile',
                    action='store',
                    required=TRUE,
                    help='Output file to store the averaged matrix in.')

args <-parser$parse_args()

tables <- lapply(args$infiles, read.table, header=TRUE, row.names=1, check.names=FALSE, sep=args$sep)
matrices <- lapply(tables, as.matrix)
stack <- abind(matrices, along=3)
stack_avg <- apply(stack, c(1,2), mean)
# Write file
write.table(stack_avg, args$outfile, sep=args$sep, col.names = NA, quote = FALSE)
cat("File written to: ", "\n", args$outfile, "\n")

生成标题:

    Helix1  Helix2  Strand1 Strand2 Turn    Unordered
20  8   8.25    18.25   9.5 13.75   36.25
....

但所需的输出是(暂时忽略这些值):

Temp    Helix1  Helix2  Strand1 Strand2 Turn    Unordered
20  2.00    4.00    21.00   11.00   19.00   43.00

示例输入矩阵可能如下所示:

Temp    Helix1  Helix2  Strand1 Strand2 Turn    Unordered
20  2.00    12.00   19.00   11.00   11.00   23.00
25  1.00    5.00    21.00   10.00   18.00   46.00
30  1.00    4.00    21.00   10.00   17.00   45.00
35  1.00    5.00    24.00   11.00   18.00   40.00
40  1.00    5.00    21.00   100.00  19.00   43.00
45  1.00    3.00    25.00   11.00   18.00   42.00
50  1.00    4.00    23.00   11.00   19.00   41.00
55  1.00    4.00    19.00   10.00   19.00   46.00
60  1.00    5.00    18.00   11.00   22.00   42.00
65  1.00    5.00    200.00  11.00   22.00   41.00
70  2.00    4.00    20.00   11.00   20.00   43.00
75  2.00    5.00    15.00   10.00   23.00   44.00
80  2.00    5.00    16.00   10.00   22.00   45.00
85  1.00    4.00    19.00   11.00   21.00   44.00
90  2.00    4.00    20.00   11.00   20.00   44.00

我怀疑您的问题出在 read.table 这一步。尝试做

test_table_read <- read.table('one_of_your_tables', 
                              header = TRUE, 
                              row.names = 1, 
                              check.names = FALSE,
                              sep = '\t')

看看View(test_table_read)。我认为此时您的行名称的 header 已经消失了。

需要考虑的一些事项:

您的行名称有何用途?它们是数字吗?如果是,也许它们应该在数据中而不是行名?

使用 data.frame 而不是 matrix 是否可以更好地解决这个问题?

顺便说一句,我本来建议给出一个更小且可重现的例子,但我认为问题确实出在你读取外部数据的地方,这使得 post.但是,如果我错了,你能用下面的一组矩阵代替你自己的矩阵来重现这个问题吗?我确实认为所有解析参数都与问题无关,可以从您的示例中删除。

matrices <- lapply(split(mtcars, 1:4), as.matrix)