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

Ben Mazzotta benjamin.mazzotta at tufts.edu
Fri Jul 9 18:49:42 CEST 2010


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?




-- 
Ben Mazzotta, PhD Candidate
The Fletcher School, Tufts University
benjamin.mazzotta at tufts.edu



More information about the R-sig-Geo mailing list