首先,我们可以使用sp包中的函数将表1和表2转换为SpatialPointsDataFrame类型,以便进行空间匹配。然后,使用sp包中的函数将表2中的每个点与表1中的最近点进行匹配,得到表2中每个点对应的站点名。最后,使用dplyr包中的函数将表1和表2根据站点名和Depth进行连接,得到最终结果,并将结果保存为csv文件。

以下是代码示例:

library(sp)
library(dplyr)

# 读取表1和表2
df1 <- read.csv('表1.csv', header = TRUE)
df2 <- read.csv('表2.csv', header = TRUE)

# 将表1和表2转换为SpatialPointsDataFrame类型
coordinates(df1) <- c('lon1', 'lat1')
proj4string(df1) <- CRS('+proj=longlat +datum=WGS84')
coordinates(df2) <- c('lon2', 'lat2')
proj4string(df2) <- CRS('+proj=longlat +datum=WGS84')

# 将表2中每个点匹配到表1中最近点的站点名
df2$nearest_site <- apply(gDistance(df2, df1, byid=TRUE), 1, function(x) df1$name[which.min(x)])

# 将表1和表2根据站点名和Depth进行连接,得到最终结果
result <- df1 %>%
  left_join(df2, by=c('name'='nearest_site', 'Depth')) %>%
  select(name, Depth, SLLL, SDUL, SSAT, SBDM)

# 将结果保存为csv文件
write.csv(result, 'E:/weidan/JIEGUO.csv', row.names=FALSE)

原文地址: https://www.cveoy.top/t/topic/nFP0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录