How can I calculate a distance matrix between columns only?

huangapple go评论93阅读模式
英文:

How can I calculate a distance matrix between columns only?

问题

我有10,000个蛋白质作为列,每个都有一个表达值。

我想要计算矩阵如下:

A B C D E F
A
B
C
D
E
F

我该如何在R中实现这个?

英文:

I have 10,000 proteins as columns and each have a expression value.

        A  B C D E F 
Value  0.1 3 4 5 6 10

I would like to calculate  the matrix as

  A B C D E F
A 
B
C
D
E
F

How can I do this in R?

答案1

得分: 2

你可以尝试像下面这样使用 dist 函数:

> x <- c(A = 0.1, B = 3, C = 4, D = 5, E = 6, F = 10)

> as.matrix(dist(x))
    A   B   C   D   E   F
A 0.0 2.9 3.9 4.9 5.9 9.9
B 2.9 0.0 1.0 2.0 3.0 7.0
C 3.9 1.0 0.0 1.0 2.0 6.0
D 4.9 2.0 1.0 0.0 1.0 5.0
E 5.9 3.0 2.0 1.0 0.0 4.0
F 9.9 7.0 6.0 5.0 4.0 0.0

这段代码用于计算数据点之间的欧几里得距离,并将结果存储在一个矩阵中。

英文:

You can try dist like below

&gt; x &lt;- c(A = 0.1, B = 3, C = 4, D = 5, E = 6, F = 10)

&gt; as.matrix(dist(x))
    A   B   C   D   E   F
A 0.0 2.9 3.9 4.9 5.9 9.9
B 2.9 0.0 1.0 2.0 3.0 7.0
C 3.9 1.0 0.0 1.0 2.0 6.0
D 4.9 2.0 1.0 0.0 1.0 5.0
E 5.9 3.0 2.0 1.0 0.0 4.0
F 9.9 7.0 6.0 5.0 4.0 0.0

huangapple
  • 本文由 发表于 2023年2月10日 04:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定