How to associate a Shapefile (.shp) attribute table input (@data) to its respective polygon ID in a Large SpatialPolygonsDataFrame in R?

Viewed 34

Thanks in advance for helping me with this issue...

When opening a shapefile (.shp) object in R using rgdal::readOGR() it results in a "S4 object of class SpatialPolygonsDataFrame". Inside that object, I have two main contents of interest: @data (list, S3: data.frame) and @polygons (list of IDs from 0 to 398). Both of those are 399 rows of lenght (because my shapefile has 399 polygons [cities]).

I'd like to correlate the Polygon ID with the corresponding line in the @data attribute table, but they are not in the same order, neither does the row number in the attribute table matches the rows of the @polygon list.

Specifically, I'd like to create a new column in the dataframe containing these polygon's IDs.

I hope I make myself understood. Thanks again for all the help!

0 Answers
Related