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

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

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

问题

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

  1. library(sf)
  2. species <- data.frame(longitude = c(-106.425668, -96.111617, -96.5,-96.5, -96.5, -96.5),
  3. latitude = c(23.18127, 15.779873, 19.68, 19.68, 19.68, 19.68),
  4. id = c("0-1","0-2","0-3","0-4","0-5","0-6"))
  5. species_sf <- st_as_sf(species, coords = c("longitude", "latitude"), crs = 4326)
  6. st_distance(species_sf)

我的输出如下:

  1. Units: [m]
  2. [,1] [,2] [,3] [,4] [,5] [,6]
  3. [1,] 0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
  4. [2,] 1357855 0.0 435620.5 435620.5 435620.5 435620.5
  5. [3,] 1098291 435620.5 0.0 0.0 0.0 0.0
  6. [4,] 1098291 435620.5 0.0 0.0 0.0 0.0
  7. [5,] 1098291 435620.5 0.0 0.0 0.0 0.0
  8. [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?

  1. library(sf)
  2. species &lt;- data.frame(longitude = c(-106.425668, -96.111617, -96.5,-96.5, -96.5, -96.5),
  3. latitude = c(23.18127, 15.779873, 19.68, 19.68, 19.68, 19.68),
  4. 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;))
  5. species_sf &lt;- st_as_sf(species, coords = c(&quot;longitude&quot;, &quot;latitude&quot;), crs = 4326)
  6. st_distance(species_sf)

My output is below:

  1. Units: [m]
  2. [,1] [,2] [,3] [,4] [,5] [,6]
  3. [1,] 0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
  4. [2,] 1357855 0.0 435620.5 435620.5 435620.5 435620.5
  5. [3,] 1098291 435620.5 0.0 0.0 0.0 0.0
  6. [4,] 1098291 435620.5 0.0 0.0 0.0 0.0
  7. [5,] 1098291 435620.5 0.0 0.0 0.0 0.0
  8. [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()来设置名称:

  1. species_sf |&gt;
  2. st_distance() |&gt;
  3. `rownames&lt;-`(species_sf$id) |&gt;
  4. `colnames&lt;-`(species_sf$id)
  5. #&gt; 单位:[m]
  6. #&gt; 0-1 0-2 0-3 0-4 0-5 0-6
  7. #&gt; 0-1 0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
  8. #&gt; 0-2 1357855 0.0 435620.5 435620.5 435620.5 435620.5
  9. #&gt; 0-3 1098291 435620.5 0.0 0.0 0.0 0.0
  10. #&gt; 0-4 1098291 435620.5 0.0 0.0 0.0 0.0
  11. #&gt; 0-5 1098291 435620.5 0.0 0.0 0.0 0.0
  12. #&gt; 0-6 1098291 435620.5 0.0 0.0 0.0 0.0
英文:

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

  1. species_sf |&gt;
  2. st_distance() |&gt;
  3. `rownames&lt;-`(species_sf$id) |&gt;
  4. `colnames&lt;-`(species_sf$id)
  5. #&gt; Units: [m]
  6. #&gt; 0-1 0-2 0-3 0-4 0-5 0-6
  7. #&gt; 0-1 0 1357855.2 1098291.3 1098291.3 1098291.3 1098291.3
  8. #&gt; 0-2 1357855 0.0 435620.5 435620.5 435620.5 435620.5
  9. #&gt; 0-3 1098291 435620.5 0.0 0.0 0.0 0.0
  10. #&gt; 0-4 1098291 435620.5 0.0 0.0 0.0 0.0
  11. #&gt; 0-5 1098291 435620.5 0.0 0.0 0.0 0.0
  12. #&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:

确定