library(osmdata) library(sf) library(sfheaders) library(geosphere) getmindist<-function(x1,y1,x2,y2){ tav <- distm(data.frame(x1,y1),data.frame(x2,y2)) apply(tav,1,min, simplify = T) } metro <- getbb("Budapest") %>% opq() %>% add_osm_feature("station","subway") %>% osmdata_sf() metro <- data.frame(metro$osm_points) metro$lat <- sapply(metro$geometry, unlist)[2,] metro$lon <- sapply(metro$geometry, unlist)[1,] vonat <- getbb("Budapest") %>% opq()%>% add_osm_feature(c("railway","train"),c("station","yes"))%>% osmdata_sf() vonat <- vonat$osm_points vonat$lat <- sapply(vonat$geometry,unlist)[2,] vonat$lon <- sapply(vonat$geometry,unlist)[1,] df$metrotav <- getmindist(df$lon,df$lat,metro$lon,metro$lat) df$vonattav <- getmindist(df$lon,df$lat,vonat$lon,vonat$lat) df$tomkoz <- apply(df[,c("vonattav","metrotav")],1,min, simplify = T)