Reputation: 1411
I am trying to calculate distances between pairs of coordinates, which is fairly easy using (for example) the gDistance function in the rgeos package. I have problems figuring out the following twist, however: In my data frame, I have information on points of interest of different types (let's say coffee shops, fast food restaurants, and bars) and I am only interested in the distances between two POIs of different types.
This is what my data frame looks like:
lat <- c(50.639342, 50.623727, 50.578924, 50.786729)
lon <- c(10.236543, 10.1896532, 10.587272, 10.776234)
type <- c("A", "A", "B", "C")
df <- data.frame(lat, lon, type)
I can calculate the distances between each pair by converting the df into a spatial object...
if (!require(sp)) install.packages('sp')
library(sp)
sp.data <- df
coordinates(sp.data) <- ~lat+lon
...and using the gDistance function to obtain a pair wise matrix of distances.
if (!require(rgeos)) install.packages('rgeos')
library(rgeos)
distance <- gDistance(sp.data, byid=T)
distance
1 2 3 4
1 0.00000000 0.04942147 0.3558949 0.5594545
2 0.04942147 0.00000000 0.4001350 0.6088076
3 0.35589488 0.40013500 0.0000000 0.2808728
4 0.55945447 0.60880759 0.2808728 0.0000000
What I would like to do next is analyze the distances between two points of different types only. For example, I am interested in what is the closest neighbor to a coffee shop that is not itself a coffee shop. My problem is that I don't know how to work with pair wise data. Ideally, I'd use the type column in the original data frame to assign NAs to all cells containing distances between points of the same type, but I cannot figure out how to do this.
Upvotes: 0
Views: 717
Reputation: 6768
You can use ?outer
, try this:
lat <- c(50.639342, 50.623727, 50.578924, 50.786729)
lon <- c(10.236543, 10.1896532, 10.587272, 10.776234)
type <- c("A", "A", "B", "C")
df <- data.frame(lat, lon, type)
library(sp)
sp.data <- df
coordinates(sp.data) <- ~lat+lon
library(rgeos)
distance <- gDistance(sp.data, byid=T)
distance
sp.data$type # we will use columns from the original data frame as you want
# solution
colnames(distance) <- sp.data$type
rownames(distance) <- sp.data$type
distance[outer(rownames(distance), colnames(distance), "==")] <- NA
distance
Upvotes: 1