开发者

spChFIDs() on level 1 or higher map-files

开发者 https://www.devze.com 2023-02-14 03:41 出处:网络
Hopefully (one of) the last question on map-files. Why is this not working, and how would I do that right?

Hopefully (one of) the last question on map-files.

Why is this not working, and how would I do that right?

load(url('http://gadm.org/data/rda/CUB_adm1.RData'))
CUB <- gadm
CUB <- spChFIDs(CUB, paste("CUB", rownames(CUB), sep = "_"))

Thank you very much!!!

seems to work w开发者_StackOverflow中文版ith row.names()

load(url('http://gadm.org/data/rda/CUB_adm1.RData'))
CUB <- gadm
CUB <- spChFIDs(CUB, paste("CUB", row.names(CUB), sep = "_"))


The answer is apparent once one reads the help for ?row.names() and ?rownames().

The rownames() function only knows something about matrix-like objects, and CUB is not one of those, hence it doesn't have row names that rownames() can find:

> rownames(CUB)
NULL

row.names() is different, it is an S3 generic function and that means package authors can write methods for specific types of objects such that the row names of those objects can be extracted.

Here is a list of the methods available for row.names() in my current session, with the sp package loaded:

> methods(row.names)
 [1] row.names.data.frame               
 [2] row.names.default                  
 [3] row.names.SpatialGrid*             
 [4] row.names.SpatialGridDataFrame*    
 [5] row.names.SpatialLines*            
 [6] row.names.SpatialLinesDataFrame*   
 [7] row.names.SpatialPixels*           
 [8] row.names.SpatialPoints*           
 [9] row.names.SpatialPointsDataFrame*  
[10] row.names.SpatialPolygons*         
[11] row.names.SpatialPolygonsDataFrame*

   Non-visible functions are asterisked

The class of the object CUB is:

> class(CUB)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

So what is happening is that the SpatialPolygonsDataFrame method of the row.names() function is being used and it knows where to find the required row names.

0

精彩评论

暂无评论...
验证码 换一张
取 消