gpt4 book ai didi

R - Spatstat - 使用数据表按 ID 搜索最近邻

转载 作者:行者123 更新时间:2023-12-02 00:47:35 24 4
gpt4 key购买 nike

我有两个名为intersections(代表街道系统的交叉路口)和users(代表网络用户)的大型数据框,如下所示:

intersections 有三列:xylabel_street。它们分别表示十字路口在方形观察窗口中的位置(比如 [0,5] x [0,5])及其所在的街道。这是一个例子:


intersections <- data.frame(x=c(0.147674, 0.235356, 0.095337, 0.147674), y=c(0.132956, 0.150813, 0.087345, 0.132956), label_street = c(5,6,5,6))

head(intersections)

x y label_street
1 0.147674 0.132956 5
2 0.235356 0.150813 6
3 0.095337 0.087345 5
4 0.147674 0.132956 6

一个十字路口位于几条街道的交叉口,intersections表中的每个(x,y)组合至少出现两次,但不同label_street(例如上例中的第 1 行和第 4 行)。 label_street 可能不是行号(这就是为什么它在我的示例中从 5 开始)。

users 有 4 列:x,y, label_street, ID .它们分别表示用户的位置、所在的街道以及每个用户唯一的ID。此数据框中没有重复项,因为用户位于唯一的街道上并且具有唯一的 ID。下面是一个示例(IDlabel_street 可能不是行号)


users <- data.frame(x = c(0.20428152, 0.17840619, 0.12964668, 0.20423856, 0.19349761, 0.10861251), y = c(0.14448448, 0.13921481, 0.11724543, 0.14447573, 0.14228827, 0.09891443), label_street = c(6,6,5,6,6,5), ID = c(2703, 3460, 4325, 12506, 19753, 21282))


head(users)
x y label_street ID
1 0.20428152 0.14448448 6 2703
2 0.17840619 0.13921481 6 3460
3 0.12964668 0.11724543 5 4325
4 0.20423856 0.14447573 6 12506
5 0.19349761 0.14228827 6 19753
6 0.10861251 0.09891443 5 21282

我想做的是:对于 intersections 的每个点 (x,y),获取 ID 和距离users

与其最近的邻居 共享相同 street_label

我有一个工作解决方案,使用 spatstat 函数 nncross 进行最近邻搜索,使用 plyr 函数 adply 进行工作在数据上。

我的工作解决方案如下:

1) 编写一个用户定义的函数,获取查询表中行的 ID 和到最近邻的距离

 NN <- function(row,query){
df <- row
window <- c(0,5,0,5) #Need this to convert to ppp objects and compute NN distance using nncross
NN <- nncross(as.ppp(row[,1:2],window),as.ppp(query[,1:2],window))
df$NN.ID <- query$ID[NN$which]
df$dist <- NN$dist
return(df)
}

2) 将这个用户定义的函数逐行应用到我的数据框“交叉点”,查询是与行共享相同街道标签的用户的子集:

result <- adply(intersections, 1, function(row) NN(row, users[users$label_street == row$label_street, ])

示例结果如下:

head(result)
x y label_street NN.ID NN.dist
1 0.147674 0.132956 5 4325 0.02391247
2 0.235356 0.150813 6 2703 0.03171236
3 0.095337 0.087345 5 21282 0.01760940
4 0.147674 0.132956 6 3460 0.03136304


由于我的真实数据帧会很大,我认为计算最近邻的距离矩阵效率不高,而且 adply 会很慢。有没有人知道类似 data.table 的解决方案?我现在才了解 data.table 的基础知识,并且一直发现它与 plyr 相比非常有效。

最佳答案

此解决方案使用 RANN 包来查找最近的邻居。诀窍是首先确保具有不同 label_street 的元素之间的距离比相同 label_street 中的元素之间的距离更大。我们通过添加一个额外的数字列来实现这一点,该列具有非常大的值,该值在同一 label_street 中是常量,但在 label_street 的不同值之间不同。总而言之,您将获得:

intersections <- data.frame(x=c(0.147674, 0.235356, 0.095337, 0.147674), y=c(0.132956, 0.150813, 0.087345, 0.132956), label_street = c(5,6,5,6))
users <- data.frame(x = c(0.20428152, 0.17840619, 0.12964668, 0.20423856, 0.19349761, 0.10861251), y = c(0.14448448, 0.13921481, 0.11724543, 0.14447573, 0.14228827, 0.09891443), label_street = c(6,6,5,6,6,5), number = c(2703, 3460, 4325, 12506, 19753, 21282))

# add a numeric column that is constant within each category and has a very large value
intersections$label_street_large <- intersections$label_street * 1e6
users$label_street_large <- users$label_street * 1e6

# call the nearest neighbour function (k = 1 neighbour)
nearest_neighbours <- RANN::nn2(
intersections[, c("x", "y", "label_street_large")],
users[, c("x", "y", "label_street_large")],
k = 1
)

# get original IDs and distances
IDs <- users$number[c(nearest_neighbours$nn.idx)]
distances <- c(nearest_neighbours$nn.dists)

IDs
# [1] 3460 12506 2703 3460 3460 4325
distances
# [1] 0.03171236 0.03136304 0.02391247 0.03175620 0.04271763 0.01760940

希望对您有所帮助。它应该非常快,因为它只调用一次 nn2,运行时间为 O(N * log(N))。

关于R - Spatstat - 使用数据表按 ID 搜索最近邻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60523855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com