Search code examples
rmapsgisshapefiler-sf

Add information from one shapefile to another shapefile in R


I have two shapefiles of the state of NSW, Australia. The first one has some regions that are useful for economic reasons, while the second has information about the koala population, in much smaller polygons.

Here I show both maps that I get when I plot these shapefiles:

Areas of NSW

Probability of finding a Koala

What I'm attempting to do is to add the region name of the first picture to each of the quadrants of the second file.

Is this possible? Which would be a method to match the polygons of both files? Thanks


Solution

  • You are likely looking for sf::st_join().

    What it does is it transfers the non-spatial information from one {sf} object to another based on spatial relation. For the function to work both spatial objects need to have the same CRS (so consider sf::st_transform() if they do not).

    Note that the order matters: you either add koala info to administrative areas, or area info to koala regions.

    Also note that some koala regions are likely to straddle the border of two regions; these will be split. So expect slightly more rows coming out that what went in.