0

I want to extract all the data attributes from my SpatialPolygonsDataframe, including the ID of all polygons.

The reason I want to do this is the following. I have a SpatiaPolygonsDataframe (which I shall name countryPolygons) that consists of regions within a country, including the names of the regions as data attributes. I have another dataframe that consists of Individual IDs with their coordinates within that country (which I shall call individuals.df). My goal is to expand the dataframe with the individuals to include a column with the name of the region that they come from. So far, I did this:

country_and_individuals.over <- over(countryPolygons, individuals.df, returnList = TRUE)  
country_and_individuals.df <- ldply(country_and_individuals.over, data.frame)

This gave me the dataframe country_and_individuals.df that consists of the columns .id (which I assume is the ID of the polygons) and the IDs of the individuals that he got from the individuals.df dataframe:

> head(country_and_individuals.df)
    .id  individual_ID
 1   0   5473277
 2   0   3054526
 3   0   3476794
 4   0   4456345
 5   0   1930378
 6   0   1345628

I reckon that if I had the .id in combination with the names of the regions that are included in the SpatialPolygonsDataframe countryPolygons under the name "NAME_2", I could merge that to country_and_individuals.df, and my goal would be achieved. How do I extract a dataframe with the columns .id and NAME_2 from the SpatialPolygonsDataframe countryPolygons?

Perhaps there is an easier way to achieve this goal (a dataframe with the individual IDs as one column and the region that they are from based on their coordinates as another column).

1

Save the SpatialPolygons as a dataframe:

countryPolygons.df <- as.data.frame(countryPolygons)

Make a variable that contains rownumbers (which are the .id variable in the country_and_individuals.df in the opening post):

countryPolygons.df$.id <- as.numeric(rownames(countryPolygons.df)) 

Merge on the variable .id:

individuals_and_regions.df <- merge(country_and_individuals.df, countryPolygons.df, by.x = ".id", by.y = ".id")
  • 1
    So, do you realize thet the @data slot is a data.frame (ie, countryPolygons@data),. and you could accomplish the same thing with sp::merge? – Jeffrey Evans Nov 18 '17 at 6:18
  • No, I did not know that! I am very new to this :) – Abdel Nov 18 '17 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.