Skip to content

primer on manipulating variables in spatial polygons data frames?

2 messages · Ben Mazzotta, Edzer Pebesma

#
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?

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?

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?
#
On 07/09/2010 06:49 PM, Ben Mazzotta wrote:
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.
vignette("sp")

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