Add Attribute Data to object of class SpatialPolygonsDataFrame in R

Here we discuss the R software package maptools, which lets us add attribute data to an object of class SpatialPolygonsDataFrame.

We can import GIS data, if stored as a shapefile, using the command gisData <- readShapePoly("NameOfShapeFile.shp").

We know it’s an object type SpatialPolygonsDataFrame from the output given by entering the command class(gisData).

We can see the attributes of this data object by following the name of the object by an @ symbol and the word data, i.e. gisData@data. The output to this command should look like a typical data set from any introductory discussion of statistics, with variables along columns and subjects across rows. In fact, we can save information from this data object for later manipulation using a command such as gDVN <- gisData@data$VariableName.

At this point modifying the attribute data of a GIS object is easy. For instance, if you wanted to change values of some variable from counts to percentages you could divide each count value of each subject on the variable of interest by the total number of counts. More concretely, we could divide the number of people in each area by the total population of the entire study region. This can be done using a command such as gisData@data$VariableName/n, where n is some number, possible the total population size.

But what if we wanted to add a new attribute to the SpatialPolygonsDataFrame object? This can be done using the function spCbind from the R software package maptools. Something like gisData <- spCbind(gisData,1:nrow(gisData)), where nrow(gisData) is the number of rows in the attribute data matrix of your GIS object, should add a column of values, ranging from 1 to nrow(gisData) to your GIS data object attribute matrix.

As always, for more information consult the help files, in this case, by entering the command ?spCbind into an active R session.


2 thoughts on “Add Attribute Data to object of class SpatialPolygonsDataFrame in R

Leave a Reply to univesaunggul Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s