0

I have a point shapefile. It has multiple points at different spatial locations. Within each point there are multiple values that I want to get the average of. In this case the new file would have one value per point.

  • 1
    Please edit the question to clarify whether there are multiple features with different values in the same location, or multiple attributes in the same feature that you want to average – Vince Jun 5 '15 at 17:29
  • There are multiple attributes in the same feature. – Geo-sp Jun 5 '15 at 18:08
  • Please edit the question to clarify which software package you would like to use to add a new column, calculate an average, and export a column subset. Either ArcGIS or R could do this, but the correct answers would be very different, which would put you in "too broad" closure territory – Vince Jun 5 '15 at 18:38
2

See How to open a Shapefile in R? for how to open your shapefile. Once you have it open, you have a SpatialPointsDataFrame with a data.frame inside that contains all your attributes. You can then perform any operation row-wise on the data.frame.

require(rgdal)

# for shapefiles, first argument of the read/write/info functions is the
# directory location, and the second is the file name without suffix     
# (From https://www.nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles)

# given layerName such as "myPoints" for a shapefile named "myPoints.shp":
shape=readOGR(workspace,layerName) 

To get access to that coveted data.frame inside, it's just shape@data. Call str on it to confirm:

str(shape@data)

should get something like this:

'data.frame':   592 obs. of  10 variables:
 $ id: ...

If you want to apply some row-wise function to the attributes and store it in another column in your data.frame it's easy.

shape@data$myMean=apply(shape@data,1,mean)

The result is a new attribute known as "myMean" (change it above to whatever you want). Of course, one of the many great things about R is that 'mean' can be anything, even a custom function, e.g.:

# totally made up, weird function just to show you can do whatever you want to each row:
# In this case it adds the field called 'income' to the square root of 'age' and adds a 
# random number between 0 and 1. Nonsense, but you can do it.
shape@data$myMean=apply(shape@data,1,function(x){ x$income * sqrt(x$age) + runif(1))

Of course, at the end of this you want to write that shapefile back out. Right now the altered data.frame is just in your R workspace. To write something out to a shapefile in the same directory with the name of "myAlteredLayer.shp":

writeOGR(shape, workspace, "myAlteredLayer", driver="ESRI Shapefile")
0

In ArcGIS, this is a simple field calculation and export if I understand the question. Add the points layer to the map and open the attribute table. Click on the top left button and choose Add Field. Give it a name and choose the appropriate data type (int/float/double). Then right-click the new field heading and choose Field Calculator. In the resulting dialog, you can select field names available and build the expression to get the average value, then click OK. With the average value calculated, you can export the layer to a new file and delete the unwanted columns, or turn them off in the layer's Properties > Fields tab and they won't be exported in the first place.

  • Thanks for your reply, the thing is the points don't have a unique id. Do you know how I can add a unique id for each point? – Geo-sp Jun 5 '15 at 22:53
  • 1
    @dnm I'm afraid I don't follow. If you have a shapefile of points, then each point should be its own record/row in the attribute table, and that's all that should be needed to do this operation. And as a shapefile it should have a FID, which is unique to each point, should be present/generated if the points are added to ArcMap. Perhaps you could post a screenshot of your points and attribute table, or describe more about why they don't have unique ids? – Chris W Jun 6 '15 at 1:16

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.