我想计算每行和两个数据帧的列之间的最小地理距离。 DF1有许多机构,DF2有许多活动。这样:
#DF1 (institutions)
DF1 <- data.frame(latitude=c(41.49532, 36.26906, 40.06599),
longitude=c(-98.77298, -101.40585, -80.72291))
DF1$institution <- letters[seq( from = 1, to = nrow(DF1))]
#DF2 (events)
DF2 <- data.frame(latitude=c(32.05, 32.62, 30.23), longitude=c(-86.82,
-87.67, -88.02))
DF2$ID <- seq_len(nrow(DF1)
我想将距离最小的事件返回到 DF1 中的每个机构,并将从 DF2 到 DF1 的距离和 ID 相加。虽然我知道如何计算成对距离,但我无法计算从 DF[1,] 到 DF2 的所有距离并返回最小值,依此类推。
这就是我尝试过的(但失败了(。
library(geosphere)
#Define a function
distanceCALC <- function(x, y) { distm(x = x, y = y,
fun = distHaversine)}
#Define vector of events
DF2_vec <- DF2[, c('longitude', 'latitude')]
#Define df to hold distances
shrtdist <- data.frame()
现在,我的尝试是用 DF1 的第 1 行和矢量化事件来馈送距离 CALC。
#Loop through every row in DF1 and calculate all the distances to instutions a, b, c. Append to DF1 smallest distance + DF2$ID.
#This only gives me the pairwise distance
for (i in nrow(DF1)){
result <- distanceCALC(DF1[i,c('longitude', 'latitude')], DF2_vec)
}
#Somehow take shortest distance for each row*column distance matrix
shrtdist <- rbind(shrtdist, min(result[,], na.rm = T))
我的猜测是,解决方案需要重塑数据并应用。此外,循环是非常糟糕的做法,考虑到观察的数量,速度太慢了。
任何帮助将不胜感激。
> 这是使用 outer
函数解决此问题的简单方法
squared_distance <- function(x, y ) (x - y)^2
lat <- outer(DF1$latitude, DF2$latitude, squared_distance)
long <- outer(DF1$longitude, DF2$longitude, squared_distance)
pairwise_dist <- sqrt(lat + long)
rownames(pairwise_dist) <- DF1$institution
colnames(pairwise_dist) <- DF2$ID
pairwise_dist
这为您提供了每个机构(行(和事件(列(之间距离的矩阵。 要获得 df1 中的距离和事件,我们可以做
df1$min_dist <- apply(pairwise_dist, 1, min)
df1$min_inst <- apply(pairwise_dist, 1, min)
请注意,在这种情况下,第二个有效的原因是事件是按数字标记的。 如果您的真实数据没有这个方便的功能,我们需要这样做
df1$min_inst <- colnames(pairwise_dist)[apply(pairwise_dist, 1, which.min)]
使用备用距离功能更新
我还没有测试过这个,但我认为这应该有效。 同样,输出将是一个矩阵。
gcd.hf <- function(DF1, DF2) {
sin2.long <- sin(outer(DF1$longitude, DF2$longitude, "-") / 2)^2
sin2.lat <- outer(DF1$latitude, DF2$latitude, "-")
cos.lat <- outer(cos(DF1$latitude), cos(DF2$latitude), "*")
a <- sin2.long + sin2.lat * cos.lat # we do this cell-wise
cir <- 2 * asin(pmin(1, sqrt(a))) # I never assign anything to "c" since that's concatenate. Rename this variable as appropriate (I have no idea if it's related to the circumference or not.)
cir * 6371
}
pairwise_dist <- gcd.hf(DF1, DF2)