[R-sig-Geo] iterative field naming

Jordan Chamberlin chamb244 at msu.edu
Thu Dec 29 00:19:27 CET 2011


Dear people,

Apologies in advance for what must be a silly question:  I want to 
iterative add a field to a dataframe, where the field takes a name that 
is created as part of the iteration. In the bit of code below, the 
problem line is "zmbpts$myfname <- extr$band1" : what I want to do is 
have the value of the string "myfname" be used as the name of the new 
field (rather than creating a new field called "myfname", which is what 
I'm doing).

for (i in 2000:2009) {
     for (j in 1:12) {

         #temporarily go to data directory, grab file, then return to 
working directory
         setwd(mydd)
         myingrid <- gsub("( )", "", paste("pre", i, "_", j))
         tmpgrid <- readGDAL(myingrid)
         setwd(mywd)

         #create SpatialPixelsDataFrame
         spdf <- as(tmpgrid, "SpatialPixelsDataFrame")
          #summary(spdf)
          #image(spdf, "band1", axes=TRUE, col=terrain.colors(20))

         #extract values to points
         proj4string(spdf)<-proj4string(zmbpts)
         extr = over(zmbpts,spdf)

         #add extracted values to SpatialPointsDataFrame
         myfname <- gsub("( )", "", paste("y", i, "m", j))
         zmbpts$myfname <- extr$band1

     }
}

Thanks for your kind help!  Jordan



More information about the R-sig-Geo mailing list