Dear List,

I am using function distCosine from package geosphere to a list of lat/lon
coordinates, and I want to calculate the great circle distance between a
pair of coordinates in the list and all other pairs --- essentially, the
output should be a matrix. I have been able to achieve this with two nested
loops, as in the example below, but this is rather slow. Can someone please
suggest how to do this with "apply" or similar?

Many thanks,
Lara


install.packages("geosphere")

library(geosphere)


##generate sets of random points

n   <- 100

lon <- runif(n, -180, 180)

lat <- runif(n, -90, 90)


#package geosphere

##spherical law of cosines method

dCos <- matrix( , nrow = length(lon), ncol = length(lat))

for (i in 1:length(lon)) {

  for (j in 1:length(lat)) {

    dCos[[i,j]] <- distCosine(matrix(c(lon[i], lat[i]), ncol=2), matrix(c(
lon[j], lat[j]), ncol=2))

}}

	[[alternative HTML version deleted]]

