0

I'm using R to manipulate an attribute table of a shapefile. The shapefile attribute table has two columns: Location ID, and Height. I want to add a third column titled "Mean", which provides the average height value by Location ID. There are over 300,000 rows in this shapefile, so this cannot be done manually.

Is there a function in R to accomplish this?

Below is an example of what the attribute table looks like. Note that the Location ID is not necessarily in sequential order; if there is an ID 1, and an ID 10, that does not mean there's an ID 2, 3, 4, etc.

 Location.ID Height 
          43      2 
          87      5  
          22      6  
          87      3  
          43      9  
          22      1  

Below is the final result I am trying to achieve in R. Note that the mean values (column 3), are of height values (column 2) specific to certain Location ID values (column 1)

 Location.ID Height Mean
          43      2  5.5
          87      5  4.0
          22      6  3.5
          87      3  4.0
          43      9  5.5
          22      1  3.5

Is there a built-in function to accomplish this?

Or is there a way to iterate through row values in column 1 to populate column 3?

closed as off-topic by Spacedman, Fran Raga, Vince, csk, BBG_GIS Jun 7 at 18:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – Spacedman, Fran Raga, Vince, csk, BBG_GIS
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Just to confirm, you have a separate table with Location.ID and Mean values you would like to join to a shapefile attributes with Location.ID and Height? – Aaron Jun 4 at 2:03
  • No, I have a table with only Location.ID and Height. I wish to add a "Mean" column that is populated by average Height values by Location.ID – Wincow Jun 4 at 2:30
  • How are you generating the mean values? – Aaron Jun 4 at 2:32
  • I don't know, that's the problem I'm trying to figure out. I need to generate mean height values by Location ID. You can see the ideal output in the second table of this post – Wincow Jun 4 at 2:56
  • 1
    Once you've read the shapefile in this is not a GIS problem. If reading the shapefile in is a problem for you, ask that. Otherwise ask general R questions on stackoverflow. – Spacedman Jun 4 at 6:48
1

You can use aggregate to summarise the table then merge the results back to the attribute table.

If you've read in 'd' using the sf package (or rgdal, though I haven't tested that):

agg <- aggregate(formula = Height ~ Location.ID, data = d, FUN = mean)
d <- merge(d, agg, by = "Location.ID")
  • This worked, thank you – Wincow Jun 4 at 14:32

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