3 回答
TA贡献1934条经验 获得超2个赞
R解决方案
#sample data: first three rows of data provided
df <- data.frame( zip = c( "00501", "00544", "00601" ),
longitude = c( -73.045075, -73.045147, -66.750909 ),
latitude = c( 40.816799, 40.817225, 18.181189 ),
stringsAsFactors = FALSE )
library( sf )
#create a spatial data.frame
spdf <- st_as_sf( x = df,
coords = c( "longitude", "latitude"),
crs = "+proj=longlat +datum=WGS84" )
#create the distance matrix (in meters), round to 0 decimals
m <- round( st_distance( spdf ), digits = 0 )
#set row and column names of matrix
colnames( m ) <- df$zip
rownames( m ) <- df$zip
#show distance matrix in meters
m
# Units: m
# 00501 00544 00601
# 00501 0 48 2580481
# 00544 48 0 2580528
# 00601 2580481 2580528 0
添加回答
举报