如何在R中使用sf包获取矩阵中的个体ID,而不是行名称?

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

How to obtain individual IDs in the matrix, instead of row names in R (sf package)?

问题

我获得了一个包含每个物种ID之间距离的矩阵。如何使矩阵显示个体的实际ID而不是行号?

library(sf)

species <- data.frame(longitude = c(-106.425668, -96.111617, -96.5,-96.5, -96.5, -96.5), 
                      latitude = c(23.18127, 15.779873, 19.68, 19.68, 19.68, 19.68), 
                      id = c("0-1","0-2","0-3","0-4","0-5","0-6"))

species_sf <- st_as_sf(species, coords = c("longitude", "latitude"), crs = 4326)

st_distance(species_sf)

我的输出如下:

Units: [m]
        [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,]       0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
[2,] 1357855       0.0  435620.5  435620.5  435620.5  435620.5
[3,] 1098291  435620.5       0.0       0.0       0.0       0.0
[4,] 1098291  435620.5       0.0       0.0       0.0       0.0
[5,] 1098291  435620.5       0.0       0.0       0.0       0.0
[6,] 1098291  435620.5       0.0       0.0       0.0       0.0

我希望方括号显示实际的物种ID。

谢谢。

英文:

I obtained a matrix of distances between each species ID. How do I get the matrix to show the actual ID of individuals instead of row numbers?

library(sf)

species &lt;- data.frame(longitude = c(-106.425668, -96.111617, -96.5,-96.5, -96.5, -96.5), 
                      latitude = c(23.18127, 15.779873, 19.68, 19.68, 19.68, 19.68), 
                      id = c(&quot;0-1&quot;,&quot;0-2&quot;,&quot;0-3&quot;,&quot;0-4&quot;,&quot;0-5&quot;,&quot;0-6&quot;))

species_sf &lt;- st_as_sf(species, coords = c(&quot;longitude&quot;, &quot;latitude&quot;), crs = 4326)

st_distance(species_sf)

My output is below:

Units: [m]
        [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,]       0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
[2,] 1357855       0.0  435620.5  435620.5  435620.5  435620.5
[3,] 1098291  435620.5       0.0       0.0       0.0       0.0
[4,] 1098291  435620.5       0.0       0.0       0.0       0.0
[5,] 1098291  435620.5       0.0       0.0       0.0       0.0
[6,] 1098291  435620.5       0.0       0.0       0.0       0.0

I want the square brackets to show actual species IDs.

Thank you.

答案1

得分: 3

你可以使用rownames()colnames()来设置名称:

species_sf |&gt;
  st_distance() |&gt;
  `rownames&lt;-`(species_sf$id) |&gt;
  `colnames&lt;-`(species_sf$id)
#&gt; 单位:[m]
#&gt;         0-1       0-2       0-3       0-4       0-5       0-6
#&gt; 0-1       0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
#&gt; 0-2 1357855       0.0  435620.5  435620.5  435620.5  435620.5
#&gt; 0-3 1098291  435620.5       0.0       0.0       0.0       0.0
#&gt; 0-4 1098291  435620.5       0.0       0.0       0.0       0.0
#&gt; 0-5 1098291  435620.5       0.0       0.0       0.0       0.0
#&gt; 0-6 1098291  435620.5       0.0       0.0       0.0       0.0

英文:

You can set names with rownames() & colnames() :

species_sf |&gt;
  st_distance() |&gt;
  `rownames&lt;-`(species_sf$id) |&gt;
  `colnames&lt;-`(species_sf$id)
#&gt; Units: [m]
#&gt;         0-1       0-2       0-3       0-4       0-5       0-6
#&gt; 0-1       0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
#&gt; 0-2 1357855       0.0  435620.5  435620.5  435620.5  435620.5
#&gt; 0-3 1098291  435620.5       0.0       0.0       0.0       0.0
#&gt; 0-4 1098291  435620.5       0.0       0.0       0.0       0.0
#&gt; 0-5 1098291  435620.5       0.0       0.0       0.0       0.0
#&gt; 0-6 1098291  435620.5       0.0       0.0       0.0       0.0

huangapple
  • 本文由 发表于 2023年8月9日 02:42:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862387.html
匿名

发表评论

匿名网友

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

确定