Invalid `.internal.selfref` warning, column not updated working with `SpatialPolygonsDataFrame`

主宰稳场 提交于 2019-12-12 00:31:58

问题


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.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or been created manually using structure() or similar). Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. Also, in R<=v3.0.2, list(DT1,DT2) copied the entire DT1 and DT2 (R's list() used to copy named objects); please upgrade to R>v3.0.2 if 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!