我正在寻找一些帮助来编写一些 R 代码以遍历数据框中的行并将每行中的值传递给函数并将输出打印到 excel 文件、txt 文件或仅在控制台中。
这样做的目的是使用此网站上的功能将一堆距离/时间查询(数百个)自动化到谷歌地图:http ://www.nfactorialanalytics.com/r-vignette-for-the-week-finding -两地之间的时间距离/
该网站上的功能如下:
library(XML)
library(RCurl)
distance2Points <- function(origin,destination){
results <- list();
xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=',origin,'&destinations=',destination,'&mode=driving&sensor=false')
xmlfile <- xmlParse(getURL(xml.url))
dist <- xmlValue(xmlChildren(xpathApply(xmlfile,"//distance")[[1]])$value)
time <- xmlValue(xmlChildren(xpathApply(xmlfile,"//duration")[[1]])$value)
distance <- as.numeric(sub(" km","",dist))
time <- as.numeric(time)/60
distance <- distance/1000
results[['time']] <- time
results[['dist']] <- distance
return(results)
}
数据框将包含两列:原始邮政编码和目的地邮政编码(加拿大,嗯?)。我是初学者 R 程序员,所以我知道如何使用 read.table 将 txt 文件加载到数据框中。我只是不确定如何遍历数据帧,每次将值传递给 distance2Points 函数并执行。我认为这可以使用 for 循环或应用调用之一来完成?
谢谢您的帮助!
编辑:
为了简单起见,假设我想将这两个向量转换为数据框
> a <- c("L5B4P2","L5B4P2")
> b <- c("M5E1E5", "A2N1T3")
> postcodetest <- data.frame(a,b)
> postcodetest
a b
1 L5B4P2 M5E1E5
2 L5B4P2 A2N1T3
我应该如何遍历这两行以从 distance2Points 函数返回距离和时间?