问题
I'm trying to do some geospatial analysis in R which will involve adding attributes to SpatialPolygonsDataFrames for coloring, etc. during plotting.
For organization, I'd like to add these attributes to my SpatialPolygonsDataFrames through a merge & update, but I keep getting the "Invalid .internal.selfref" warning & the columns won't be added.
Poking around the questions and answers here, it seems to be related to the fact that the data for a SpatialPolygonsDataFrame object is stored in a list, but the answers there were no help for how to deal with this as they were generally dealing with user-defined lists instead of those coming out of a package like here.
Here's a simple example using a silly shapefile, say U.S. States (you'll note I use the 500k resolution below):
library(maptools)
us.states<-readShapePoly("cb_2014_us_state_5m.shp")
setDT(us.states@data) #works fine
> class(us.states@data)
[1] "data.table" "data.frame"
us.states@data[,test:=1L]
Warning message: In
`[.data.table`(us.states@data, , `:=`(test, 1L)): Invalid.internal.selfrefdetected and fixed by taking a (shallow) copy of thedata.tableso that:=can add this new column by reference. At an earlier point, thisdata.tablehas been copied byR(or been created manually usingstructure()or similar). Avoidkey<-,names<-andattr<-which inRcurrently (and oddly) may copy the wholedata.table. Useset*syntax instead to avoid copying:?set,?setnamesand?setattr. Also, inR<=v3.0.2,list(DT1,DT2)copied the entireDT1andDT2(R'slist()used to copy named objects); please upgrade toR>v3.0.2if that is biting. If this message doesn't help, please report to datatable-help so the root cause can be fixed.
This sort of jives with what I've gathered from the other related answers, but I at least expected the data to be updated, but alas:
> names(us.states@data)
[1] "STATEFP" "STATENS" "AFFGEOID" "GEOID" "STUSPS" "NAME"
"LSAD" "ALAND" "AWATER"
Is there any way I can continue to use the comfortable := update by reference syntax for working with a SpatialPolygonsDataFrame (or similar .shp-retrieved object)?
For now, I'm using updates by copying, which works, e.g.:
us.states@data<-copy(us.states@data)[,test:=1L]
> names(us.states@data)
[1] "STATEFP" "STATENS" "AFFGEOID" "GEOID" "STUSPS" "NAME" "LSAD"
[8] "ALAND" "AWATER" "test"
来源:https://stackoverflow.com/questions/32380338/invalid-internal-selfref-warning-column-not-updated-working-with-spatialpol