Skip to content

change a value in the dataframe of a SPDF

2 messages · Ingo Holz, Roger Bivand

#
Hi,

 I changed a specific value in the dataframe of a SpatialPolygonsDataFrame 
in the following way:

 SPDF[["Name"]][1] <- 1

 This needed a very long time (about 10 seconds).

 I have two questions:
   Is there a faster way to do it?
   Why did it need that much time like I did it?

 Thank you,
 Ingo
#
On Wed, 7 Nov 2007, Ingo Holz wrote:

            
When assigning to an existing object, in general the whole object gets 
recreated. With the North Carolina 100 county data set, both your version 
and the equivalent SPDF$Name[1] <- 1 take the same time, and the time is 
minimal (100 counties, 14 variables). If you can make the data available 
privately, and I imagine that the sizes are >> NC, I can see whether 
profiling provides any answers. With hundreds of thousands of polygons, 
you might see substantial time being used, because the memory model does 
not just "drop" the replacement value into the data.frame in the SPDF data 
slot. Is:

tmp <- SPDF[["Name"]]
tmp[1] <- 1
SPDF[["Name"]] <- tmp

faster, slower? Is assigning to a new column name faster or slower? Note 
that there are two nested access functions here, the [[]] and the [] - as 
you probably noticed, trying to replace [1, "Name"] doesn't work because 
there is no [,] <- replacement method.

Roger