I'm just beginning to work with R and spatial analysis, so there is a lot of basic knowledge I'm missing!
My data contains addresses of schools. For every school, I want to calculate the distance to the nearest school. My data also contains information about "special features" of the schools. This variable (sps) is coded 1 ("with special features") or 0 ("without special features"). I want to calculate distances from schools "with special features" to schools "without special features". This is how my data looks like:
head(data01)
id lon lat sps
1 11725 6.932546 50.38269 0
2 11739 6.975160 50.48649 1
3 26883 6.987575 50.50857 0
So far, I managed to calculate the distance to the nearest school using the st_distance commend and the following code. Unfortunately, when using this code, in some cases the nearest schools both have "special features". I only want the distance from 1 -> 0 or 0 ->1 and not 1->1!
my_sf <- st_as_sf(data01,
coords = c("lon", "lat"), # x, y (order matters)
crs = 4326)
dist.mat <- st_distance(my_sf) # Great Circle distance since in lat/lon
# Number within 1.5km: Subtract 1 to exclude the point itself
num.1500 <- apply(dist.mat, 1, function(x) {
sum(x < 1500) - 1
})
# Calculate nearest distance
nn.dist <- apply(dist.mat, 1, function(x) {
return(sort(x, partial = 2)[2])
})
# Get index for nearest distance
nn.index <- apply(dist.mat, 1, function(x) { order(x, decreasing=F)[2] })
n.data <- data01
colnames(n.data)[1] <- "neighbor"
colnames(n.data)[2:ncol(n.data)] <-
paste0("n.", colnames(n.data)[2:ncol(n.data)])
mydata2 <- data.frame(data01,
n.data[nn.index, ],
n.distance = nn.dist,
radius1500 = num.1500)
rownames(mydata2) <- seq(nrow(mydata2))
Thanks for helping!!
cheers, k
edit:
My final dataset should look like this:
head(data01)
id lon lat sps dist
11725 6.932546 50.38269 0 xxxx
11739 6.975160 50.48649 1 xxxx
26883 6.987575 50.50857 0 xxxx
Dist would be the distance to the next school (0-> 1 or 1 -> 0)