It took me quite a while to find this, so I thought it would be good to post. You firstly need to install the library "geotools", and then use distKm to get the distance between your two coordinates. It spits out the distance in km.
library(geotools)
distKm(lat1,long1,lat2,long2)
However, I had a list of 10 x sites that I wanted to compare to 5 y sites (so a total of 50 combinations). I am sure there is a much prettier way to do this, but this is how I managed it...
This would use 2 data sets - x.sites and y.sites. Each would have a column for latitude and longitude. You should also have the site names to make it clear what you are looking at. Here, x.sites is used to go through each row/site for the for loop. The data are set up as follows:
Site Latitude Longitude
C01 54.6 4.98
# set up a blank thing called env to count which round we are up to, need to do this so that the first loop #actually starts the new data set of results, and all others just add on as more rows.
env <- new.env()
env$count <- 1
#For loop to go through each of the 10 x site to get the distances against the other 5 y sites
for(i in 1:nrow(x.sites)) {
#Set up the coordinates for that row/site for the x
lat1<-x.sites[i,2]
long1<-x.sites[i,3]
#Create a new dataset "temporary" with the distance
temporary<-as.data.frame(distKm(lat1,long1,y.sites$Lat,y.sites$Long))
#Add in the site names
temporary$x.site<-x.sites[i,1]
temporary$y.site<-y.sites$sitename
if(env$count == 1){
Site.dist <- temporary
} else {
Site.dist <- rbind(Site.dist, temporary)
}
## clean up
try(rm(lat1,long1))
env$count <- env$count + 1
} ## end of loop over species
You then end up with the data frame Site.dist which looks like this for each site combo,
Distance x.site y.site
43km C01 Y01
library(geotools)
distKm(lat1,long1,lat2,long2)
However, I had a list of 10 x sites that I wanted to compare to 5 y sites (so a total of 50 combinations). I am sure there is a much prettier way to do this, but this is how I managed it...
This would use 2 data sets - x.sites and y.sites. Each would have a column for latitude and longitude. You should also have the site names to make it clear what you are looking at. Here, x.sites is used to go through each row/site for the for loop. The data are set up as follows:
Site Latitude Longitude
C01 54.6 4.98
# set up a blank thing called env to count which round we are up to, need to do this so that the first loop #actually starts the new data set of results, and all others just add on as more rows.
env <- new.env()
env$count <- 1
#For loop to go through each of the 10 x site to get the distances against the other 5 y sites
for(i in 1:nrow(x.sites)) {
#Set up the coordinates for that row/site for the x
lat1<-x.sites[i,2]
long1<-x.sites[i,3]
#Create a new dataset "temporary" with the distance
temporary<-as.data.frame(distKm(lat1,long1,y.sites$Lat,y.sites$Long))
#Add in the site names
temporary$x.site<-x.sites[i,1]
temporary$y.site<-y.sites$sitename
if(env$count == 1){
Site.dist <- temporary
} else {
Site.dist <- rbind(Site.dist, temporary)
}
## clean up
try(rm(lat1,long1))
env$count <- env$count + 1
} ## end of loop over species
You then end up with the data frame Site.dist which looks like this for each site combo,
Distance x.site y.site
43km C01 Y01