使用tidyverse 和sf,要获得以米为单位的距离矩阵,请尝试:
library(tidyverse)
library(sf)
df <- tibble(
Unique_ID = c(45L, 49L, 70L, 71L),
EW_X = c(573500L, 572224L, 573573L, 573573L),
EW_Y = c(775011L, 774700L, 775200L, 775200L))
sf_trees <- st_as_sf(x = df, coords = c("EW_X", "EW_Y"),
crs = 3857) # Note that I am using WGS 84 Pseudo Mercator. If you know the UTM zone you could be more specific with the csr/epsg code
tb_distance <- st_distance(sf_trees, sf_trees, ) %>% # The following 3 lines are optional.
as_tibble() %>%
set_names(nm = df$Unique_ID) %>%
bind_cols(tibble(Unique_ID = as.character(df$Unique_ID)), . )
tb_distance
# A tibble: 4 x 5
Unique_ID `45` `49` `70` `71`
<chr> [m] [m] [m] [m]
1 45 0 1313. 203. 203.
2 49 1313. 0 1439. 1439.
3 70 203. 1439. 0 0
4 71 203. 1439. 0 0