优化R中大df的距离distHaversine模型

时间:2017-02-21 18:19:07

标签: r optimization haversine

我正在使用大型数据集,我正在尝试在具有8GB RAM的本地计算机上运行地理空间分析。看起来我已经超出了我的机器的资源,我想知道我是否可以优化我的模型,所以我可以在我的机器上运行它。

area <- data.frame(area = c('Baker Street','Bank'),
                  lat = c(51.522236,51.5134047),
                  lng = c(-0.157080, -0.08905843),
                  radius = c(100,2000)
)

stop <- data.frame(station = c('Angel','Barbican','Barons Court','Bayswater'),
                   lat = c(51.53253,51.520865,51.490281,51.51224),
                   lng = c(-0.10579,-0.097758,-0.214340,-0.187569),
                   postcode = c('EC1V','EC1A', 'W14', 'W2'))



library(geosphere)


datNew = lapply(1:nrow(area), function(i) {

  df = stop

  df$dist = distHaversine(df[,c("lng", "lat")], 
                          area[rep(i,nrow(df)), c('lng','lat')])

  df$in_circle = ifelse(df$dist <= area[i, "radius"], "Yes", "No")

  df$circle_id = area[i, "area"]

  df

})

datNew = do.call(rbind, datNew)

require(dplyr)    
datNew  <- datNew %>% 
  group_by(station) %>% 
  slice(which.min(dist))

是否可以计算距离,然后在station station之间找到最小距离,这样我就不会将stations的数量乘以数量area?或者是否有另一种解决方案可以让我以较少资源消耗的方式运行它或分割作业以使其适合RAM?

1 个答案:

答案 0 :(得分:1)

你是否尝试过将gc()放在lapply函数的末尾?它为下一次迭代释放了内存空间。如果这没有帮助生病尝试回到这个答案tommorow,请回复:)

编辑:

我不知道你是否考虑到了这一点,但是你走了:

library(geosphere)
library("plyr")
library("magrittr")

area <- data.frame(area = c('Baker Street','Bank'),
                   lat = c(51.522236,51.5134047),
                   lng = c(-0.157080, -0.08905843),
                   radius = c(100,2000)
)

stop <- data.frame(station = c('Angel','Barbican','Barons Court','Bayswater'),
                   lat = c(51.53253,51.520865,51.490281,51.51224),
                   lng = c(-0.10579,-0.097758,-0.214340,-0.187569),
                   postcode = c('EC1V','EC1A', 'W14', 'W2'))

## In the function below you take an area one by one and then save the station which at the minimal
## distance from the given area

min.dist <- ddply(area, ~area, function(xframe){

  xframe <<- xframe
  cat("Calculating minimum distance from area...", as.character(xframe$area), "\n")

  dists <- distHaversine(xframe[, c("lat", "lng")], stop[ , c("lat", "lng")]) 
  stop.min <- stop[which(min(dists)==dists), ]
  stop.min$area <- xframe$area
  return(stop.min)
  gc()

})

min.dist # the new data frame