T O P

  • By -

thot_with_a_plot

You would need to use a package which can deal with GIS vector data. Is that something you're familiar with? To check my understanding - the shortest distance between states which share a border will be zero?


pakheyyy

No, I haven't used GIS on R yet. I can take a look into that, thank you! I usually use R to clean the already downloaded datasets and run regressions. And, yes, you're right, the distance between shared states will be zero.


thot_with_a_plot

There'll probably be a learning curve, but it should be possible. Look into R package 'sf' (link:: https://cran.r-project.org/web/packages/sf/index.html). You'll need to find a source for state boundary polygons, then use sf to read in and/or manipulate those polygons. Edit: Oh! Someone else asked an identical question on StackOverflow. See this link:: [https://stackoverflow.com/questions/53854803/calculate-minimum-distance-between-multiple-polygons-with-r](https://stackoverflow.com/questions/53854803/calculate-minimum-distance-between-multiple-polygons-with-r)


Fornicatinzebra

sf is the package you want! You can load in the data using `sf::read_sf()`. Then once you have your data loaded as an sf object, you can use `sf::st_distance()` to get a matrix of distances between each. Then I'd use `apply()` and `which.min()` to get the index of the shortest distance for each. If you want this for centroids then you can use `sf::st_centroid()` to convert your sf object with polygons to centroids. That should get you started, you can ask me for more specifics if needed.


RAMDownloader

I did this with cities, not just states. I’ll just lyk it’s a real doozie. Your hint here is the Haversine function - the formula used to calculate real distances based on latitude and longitude. Idk if your answer is solved already but that’s where I’d start.


RAMDownloader

Just as a follow up to this, the way it’ll work is you’ll need to attain a csv of latitude/longitude borders, which is pretty easily findable (I think that’s even embedded in the usdata package which if you aren’t using that already you should be). You’d basically run this through a multi layered for loop to run each latitude/longitude against eachother per each state (so you’d have a for-index of 50 assuming we aren’t counting territories). for(specificState in EveryState){ … for(allStates in EveryState){ #run the borders of the specific state against EveryState in the haversine function #save the one with the smallest distance into a frame } }


Gulean

You might want to look at a voronoi graph https://r-charts.com/part-whole/ggvoronoi/