是否有R函数可以代替使用几个数据帧的几列的循环?

时间:2019-05-25 12:39:45

标签: r loops distance

目标是计算高速公路沿线所有交通柜台与比利时所有加油站之间的距离。所以我需要每个柜台到每个车站的距离。在比利时数据框中,您可以找到柜台的纵向和横向距离;在数据站中,您可以找到加油站的纵向距离。

目前,我使用了for循环,这对于小型数据帧而言效果很好,但对于大型数据帧而言却非常慢,这是循环的一个特征。

Stations1<-Stations[,c("lon","lat")] names(Stations1)<-NULL BELGIUM1<-BELGIUM[,c("lon","lat")] names(BELGIUM1)<-NULL

distancesToStation <- data.frame(matrix(NA,nrow = nrow(Stations),ncol = nrow(BELGIUM)))

     for (i in 1:nrow(BELGIUM)) {
     for (j in 1:nrow(Stations)){
          distancesToStation[j,i] = gmapsdistance(origin = 
                 paste0(Stations1[j,1],"+",Stations1[j,2]),
                 destination =  
                           paste0(BELGIUM1[i,1],"+",BELGIUM1[i,2]),
                 mode = "driving",key = "X")[[2]]/1000 

}}

save(distancesToStation, file = 'DistanceMatrix.Rdata')

此代码非常适合小型数据帧,是否可以加快速度?

1 个答案:

答案 0 :(得分:0)

这首先使用交叉连接merge(..., ..., by = NULL)生成所有连击,然后仅对gmapsdistance使用向量化方法。请注意,我没有API或其他任何东西,所以我无法测试该部分。

BELGIUM <- data.frame(counters = 1:10
                      , lat = runif(10, 10, 20)
                      , lon = runif(10, 40, 50))

STATIONS <- data.frame(station = LETTERS[1:10]
                       , lat = runif(10, 10, 20)
                       , lon = runif(10, 40, 50))

All_Combos <- merge(BELGIUM, STATIONS, by = NULL)

All_Combos$distancesToStation = gmapsdistance(origin = paste0(All_Combos$lat.y,"+",All_Combos$lon.y),
                                   destination =  paste0(All_Combos$lat.x,"+",All_Combos$lat.x),
                                   mode = "driving",key = "X")[[2]]/1000