通过减去行和列名称分隔符来创建data.frame

时间:2016-09-24 02:56:42

标签: r

我有一个距离矩阵,其行和列由在第一个下划线之后表示的数值界定(例如,7A_0_AAGCCTAGCGAC = 0)。我想要一种方法来比较这些值的行与列方式。比方说,我想从列分隔符中减去行分隔符。

输入:

                  7A_0_AAGCCTAGCGAC 7A_4_AAATGACTGGCC 7A_7_CATCTCGTTCTA
7A_0_AAGCCTAGCGAC        0.00000000       0.034312102        0.04539427
7A_4_AAATGACTGGCC        0.03431210       0.000000000        0.01422137
7A_7_CATCTCGTTCTA        0.04539427       0.014221369        0.00000000

预期产出:

                  7A_0_AAGCCTAGCGAC 7A_4_AAATGACTGGCC 7A_7_CATCTCGTTCTA
7A_0_AAGCCTAGCGAC        0.00000000                -4                -7
7A_4_AAATGACTGGCC                 4       0.000000000                -3
7A_7_CATCTCGTTCTA                 7                 3        0.00000000

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以分别从列名和行名中提取数值,然后执行外部减法:

# extract numeric values from the dimension names of the matrix
cols = as.numeric(sub(".*_(\\d+)_.*", "\\1", colnames(mat)))
rows = as.numeric(sub(".*_(\\d+)_.*", "\\1", rownames(mat)))

# calculate an outer subtract from the two vectors
output <- outer(cols, rows, "-")

# set up the dimension name
dimnames(output) <- list(rownames(mat), colnames(mat))

output
#                  7A_0_AAGCCTAGCGAC 7A_4_AAATGACTGGCC 7A_7_CATCTCGTTCTA
#7A_0_AAGCCTAGCGAC                 0                -4                -7
#7A_4_AAATGACTGGCC                 4                 0                -3
#7A_7_CATCTCGTTCTA                 7                 3                 0