Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

r - Using the geosphere distm function on a data.table to calculate distances

I've created a data.table in that has 6 columns. My data.table has a columns compairing two locations: Location 1 and Location 2. I'm trying to use the distm function to calculate the distance between the locations on each row, creating a 7th column. The distm package in the geosphere package requires two different vectors for each lat/long combo to be calculated against. My code below does not work, so I'm trying to figure out how to provide vectors to the function.

LOC_1_ID LOC1_LAT_CORD LOC1_LONG_CORD LOC_2_ID LOC2_LAT_CORD LOC2_LONG_CORD
 1       35.68440        -80.48090        70624    34.86752   -82.46632
 6       35.49770        -80.62870        70624    34.86752   -82.46632
10       35.66042        -80.50053        70624    34.86752   -82.46632

Assuming res holds the data.table the below code does not work.

 res[,DISTANCE := distm(c(LOC1_LAT_CORD, LOC1_LONG_CORD),c(LOC2_LAT_CORD, LOC2_LONG_CORD), fun=distHaversine)*0.000621371]

If I were to pull out each vector the function works fine.

loc1 <- res[LOC1_ID == 1,.(LOC1_LAT_CORD, LOC1_LONG_CORD)]
loc2 <- res[LOC2_ID==70624,.(LOC2_LAT_CORD, LOC2_LONG_CORD)]
distm(loc1, loc2, fun=distHaversine)

Really, my question is how to apply functions to select columns within a data.table when that function requires vectors as parameters.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The distm fucntion generates a Distance matrix of a set of points. Are you sure this is the function you want if you're just comparing the points on each row, and adding one column?

It sounds like you actually want either distHaversine or distGeo

library(data.table)
library(geosphere)

dt <- read.table(text = "LOC_1_ID LOC1_LAT_CORD LOC1_LONG_CORD LOC_2_ID LOC2_LAT_CORD LOC2_LONG_CORD
1       35.68440        -80.48090        70624    34.86752   -82.46632
6       35.49770        -80.62870        70624    34.86752   -82.46632
10       35.66042        -80.50053        70624    34.86752   -82.46632", header = T)

setDT(dt)
dt[, distance_hav := distHaversine(matrix(c(LOC1_LONG_CORD, LOC1_LAT_CORD), ncol = 2),
                                   matrix(c(LOC2_LONG_CORD, LOC2_LAT_CORD), ncol = 2))]

#     LOC_1_ID LOC1_LAT_CORD LOC1_LONG_CORD LOC_2_ID LOC2_LAT_CORD LOC2_LONG_CORD distance_hav
# 1:        1      35.68440      -80.48090    70624      34.86752      -82.46632     202046.3
# 2:        6      35.49770      -80.62870    70624      34.86752      -82.46632     181310.0
# 3:       10      35.66042      -80.50053    70624      34.86752      -82.46632     199282.1

Update: This answer gives a more efficient version of distHaversine for use in data.table


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...