a_a_15
a_a_15

Reputation: 25

Is there any way I can optimize this R code?

This is a code I'm trying to run in rstudio. I know the iterations are way too long. Is there any optimal/faster way to do this? I've been stuck for 4+ hours and it doesn't seem like finishing any time soon.

I'm trying to make a distance matrix between 415 cities and 3680126 monuments. To optimize, I am only comparing those monuments with cities which are present in the same country.

for(x in 1:3680126){
  for(y in 1:415){
    if(list2_cities$Country[y]==list1_POI$Country[x]){
      distance_matrix [x,y] <- ({POI$Longitude[x]-cities$Longitude[y]}^2)+({POI$Latitude[x]-cities$Latitude[y]}^2)
    }
    else{
      distance_matrix [x,y] <- 0
    }
  }
}

Upvotes: 1

Views: 88

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101335

Maybe you can try distm from package geosphere

library(geosphere)
d <- distm(list1_POI[c("Longitude","Latitude")],list2_cities[c("Longitude","Latitude")])
m <- +(outer(list1_POI$Country,list2_cities$Country,`==`))
res <- d*m

where

  • the distm part gives the all paired distances between two cities
  • the outer part provides a mask such that values for non-matched cities are set to 0

If your desired matrix is sparse, here is another option

common <- intersect(list1_POI$Country,list2_cities$Country)
rl <- match(common,list1_POI$Country)
cl <- match(common,list2_cities$Country)
d <- diag(distm(list1_POI[rl,c("Longitude","Latitude")],list2_cities[cl,c("Longitude","Latitude")]))
res <- matrix(0,length(list1_POI$Country),length(list1_cities$Country))
res[cbind(rl,cl)] <- d

where you only need to locate the matched cities and calculate their distances.

Upvotes: 1

Related Questions