在循环中列出结果 - gmapsdistance

时间:2017-05-09 23:55:48

标签: r loops dataframe

我正在尝试使用R包 gmapsdistance 来计算两组坐标之间的道路距离和行程时间。我创建了以下循环,因为我有2524条路线要计算。

for(i in 1:2524) {
df$Distance_Time[i] <- as.data.frame(gmapsdistance
                              (origin = c(df$OriginCo[i]), 
                              destination = c(df$DestinationCo[i]), 
                              combinations = "pairwise", 
                              mode = "driving",
                              shape = "long",
                              set.api.key("MY_API_KEY"))) 
}

我的df的一个例子如下;

OriginCo:chr&#34; -39.9 + 174.99&#34;,&#34; -40.27 + 175.55&#34;,&#34; -39.66 + 176.77&#34; ...

DestinationCo:chr&#34; -40.27 + 175.55&#34;,&#34; -39.9 + 174.99&#34;,&#34; -40.27 + 175.55&#34; ...

Distance_Time:

但是,由于 gmapdistance 的输出是一个列表,因此df(Distance_time)中的新列仅显示每组坐标的行程时间。

如果我在第一组坐标上运行代码,而不是循环,我得到$ time,$ distance和$ status。

如何创建另一列以显示距离和状态,或者将这两个变量与$ time一起列出?

非常感谢

0 个答案:

没有答案