我有以下格式的数据(行数:~ 100 万)
head(dt)
pickup_longitude pickup_latitude dropoff_longitude dropoff_latitude
1: -74.00394 40.74289 -73.99337 40.73425
2: -73.97386 40.75219 -73.95870 40.77253
3: -73.95441 40.76442 -73.97078 40.75835
4: -73.96234 40.76722 -73.97551 40.75687
5: -74.00466 40.70743 -73.99937 40.72152
6: -73.99557 40.71602 -73.99997 40.74332
library(geosphere)
dt = data.table(pickup_longitude = c(-74.00394, -73.97386, -73.95441, -73.96234, -74.00466, -73.99557),
pickup_latitude = c(40.74289, 40.75219, 40.76442, 40.76722, 40.70743, 40.71602),
dropoff_longitude = c(-73.99337, -73.95870, -73.97078, -73.97551, -73.99937, -73.99997),
dropoff_latitude = c(40.73425, 40.77253, 40.75835, 40.75687, 40.72152, 40.74332))
dt[, distance := apply(dt, 1, function(t) distm(x = c(t[1], t[2]), y = c(t[3], t[4])))]
我已经使用了上面的代码,因为包中apply
的函数没有向量化。但是,上面的代码需要很多时间。distm
geosphere
apply
我也试过:
dt[, distance := distm(x = c(pickup_longitude, pickup_latitude), y = c(dropoff_longitude, dropoff_latitude)), by = 1:nrow(dt)]
还有什么可以更好更快地计算距离的方法?