[R-sig-Geo] primer on manipulating variables in spatial polygons data frames?

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat Jul 10 17:13:24 CEST 2010



On 07/09/2010 06:49 PM, Ben Mazzotta wrote:
> Do spatial polygon data frames have an equivalent of subscripting for
> data frames? Or perhaps a way to call variables akin to the get() command?

as with data.frames, for a SpatiaPolygonsDataFrame x, x["attr"] selects
the attribute, and x[1,] selects the first polygons associated with the
first entry in the attribute table.

> 
> I'd like to be able to write apply() functions over the variables in a
> spatial polygons data frame, so that I can automate the generation of
> plots.
> 
> Roger et al. have done such great work on maptools() and related
> packages, I imagine this must be straightforward; but I can't find the
> documentation for it.  Can someone point me in the right direction?
> 

vignette("sp")

or for a more extensive treatment the book "Applied spatial data
analysis with R" written by Roger et al.

> Examples w/ comments below.
> 
> Thank you-
> Ben
> 
> # This is a test of my ability to call columns of a spatial data frame.
> 
> library(maptools)
> data(wrld_simpl)
> 
> df.obs <- nrow(wrld_simpl)*10 #enough data for ten rows
> 
> df <- runif(df.obs,1,10)    #create some data
> df <- data.frame(matrix(df, nrow(wrld_simpl),10)) #slice it up
> 
> row.names(df) <- row.names(wrld_simpl)
> 
> newworld <- spCbind(wrld_simpl, df)
> names(newworld)
> 
> # It is easy to call a column of the data by name.
> 
> class(newworld)
> class(newworld$X1)
> mean(newworld$X1)
> 
> # What about subscripts?
> # The spatial polygons data frame doesn't have the right number of
> dimentions. :(
> class(newworld[,1])
> 
> # Get commands?
> varname <- "newworld$X1" #This is a variable name
> class(get(varname))    #But the get command can't find it....
> 
> # How can I write an apply()-family command over the variables in the
> newworld spatial polygons data frame?
> 
> 
> 
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list