我想用基因表达数据制作一个平方关系矩阵。我的数据子集是这样的:
gene. Sample1 Sample2 Sample3 Sample4 Sample5
A 13.932431 5.366284 6.93992 6.818242 2.564284
B 21.111017 0.662061 1.563687 2.135377 0.261206
C 26.471751 0.932416 1.673144 1.606691 0.361993
D 27.597507 36.591138 28.371248 31.376842 30.893555
E 35.324703 0 1.462438 1.175831 0
F. 7.381737 3.083751 4.56243 6.64035 5.346179
G 20.689661 58.773005 29.078037 69.195561 58.661982
H 0 0 0 0 0
I 0 0 0 0 0我想用这个公式:X%*%X / ncol(X)。我想要一个矩阵,它的行数等于列数,而行数应该等于个体数。我总共有500个样本和35000个基因。
发布于 2020-07-17 14:09:01
你不能把X乘以X。也许你想要这个?
让我们假设您从.csv文件中导入的数据位于名为data的data.frame中。您需要删除包含基因符号的字符列,然后可以将矩阵的转置与其本身相乘。
mat <- as.matrix(data[,!names(data) %in% "gene"])
(t(mat) %*% mat) / nrow(mat)
Sample1 Sample2 Sample3 Sample4 Sample5
Sample1 425.8389 262.4451 182.6579 285.6350 239.6188
Sample2 262.4451 536.9769 311.2274 586.1026 512.1032
Sample3 182.6579 311.2274 191.8691 331.9591 291.7184
Sample4 285.6350 586.1026 331.9591 652.4043 564.7350
Sample5 239.6188 512.1032 291.7184 564.7350 492.3329样本数据
data <- structure(list(gene = structure(1:9, .Label = c("A", "B", "C",
"D", "E", "F", "G", "H", "I"), class = "factor"), Sample1 = c(13.932431,
21.111017, 26.471751, 27.597507, 35.324703, 7.381737, 20.689661,
0, 0), Sample2 = c(5.366284, 0.662061, 0.932416, 36.591138, 0,
3.083751, 58.773005, 0, 0), Sample3 = c(6.93992, 1.563687, 1.673144,
28.371248, 1.462438, 4.56243, 29.078037, 0, 0), Sample4 = c(6.818242,
2.135377, 1.606691, 31.376842, 1.175831, 6.64035, 69.195561,
0, 0), Sample5 = c(2.564284, 0.261206, 0.361993, 30.893555, 0,
5.346179, 58.661982, 0, 0)), class = "data.frame", row.names = c(NA,
-9L))https://stackoverflow.com/questions/62955550
复制相似问题