数据帧中每一行的gmapsdistance

时间:2019-03-14 13:26:15

标签: r

我试图获取两组位置的行驶时间和距离,均以经度和纬度表示。 我使用gmapsdistance之前,它将返回两个向量中每个点的距离矩阵。但是在这里我只想计算每一行的距离。因此,我考虑对每一行使用apply函数。

数据看起来像这样

 unique_pair <- structure(list(latitude = c(42.018435, 42.018435, 42.120501), longitude = c(-70.846707, -70.846707, -71.233164),x.x.locationlatitude = c(42.23656924, 42.361856, 41.901186), x.x.locationlongitude = c(-70.9865341, -71.065615, -71.093756), n = c(80L, 2L, 65L)), row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame"))


abk<-apply(unique_pair,1,function(x) { gmapsdistance(
    origin =x[1]+x[2],  # start point of journey
    destination = x[3]+x[4],  # end point of journey
    mode = "driving",  # driving time
    shape = "long"  # format of output data 
    )})

[1,2] [3,4]是两个位置的经度和纬度。但是总会有这样的错误

如果我不使用+,他们将lon和lat分别用作位置,并报告结果无用。

如果我使用+将lat和lon作为位置点,它将对二进制运算符显示非数字参数

我也考虑引用它们,但这也不起作用。

谁能提供帮助并提供一些建议?非常感谢

0 个答案:

没有答案