我有以下数据框:
table <- data.frame(pop_1 = c("AL","AL","AL","AL","AL","AL","AL","ALT","ALT","ALT","ALT","ALT","ALT","BU","BU","BU","BU","BU","IRK","IRK","IRK","IRK","KK","KK","KK","KYA","KYA","TU"),
pop_2 = c("ALT","BU","IRK","KK","KYA","TU","ZAB","BU","IRK","KK","KYA","TU","ZAB","IRK","KK","KYA","TU","ZAB","KK","KYA","TU","ZAB","KYA","TU","ZAB","TU","ZAB","ZAB"),
value = c(0.43447,0.15267,0.25912,0.10435,0.19238,0.19186,0.18155,0.34969,0.07506,0.29206,0.13597,0.46354,0.17870,0.18658,0.02297,0.08851,0.18950,0.05176,0.12086,0.02690,0.29669,0.05551,0.04910,0.15779,0.03276,0.23422,0.00568,0.22181))
如何将其转换为具有空(或NA等)单元格的非对称矩阵,如下所示:
2条答案
按热度按时间qnakjoqk1#
对你的数据框做一些小的改动,在开始处多加一个"AL","AL","NA"组合。你也可以在结尾处多加一个"ZAB":
编辑:
dxxyhpgq2#
创建一个包含数据框的pop_1和pop_2列中所有唯一值的向量。这将是矩阵的行和列的名称。
使用matrix函数创建一个空矩阵,其行数和列数与步骤1中的向量相同。使用value参数将矩阵的默认值设置为NA。
使用rownames和colnames函数将矩阵的行和列的名称设置为population向量中的值。
使用for循环迭代数据框的行。对于每一行,使用pop_1和pop_2列查找矩阵中对应的单元格,并使用value列设置这些单元格的值。
在这些步骤之后,矩阵应该是非对称矩阵,其中来自 Dataframe 的值在适当的单元中,并且NA在所有其它单元中。
当您查看矩阵的结果时: