是否可以以某种方式轻松处理Spatial*DataFrame
(* = 点、线、多边形、像素、网格...)中的数据?特别是我很难为它分配值并使用它们进行操作:
require(gstat)
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.grid)
gridded(meuse.grid) = ~x+y
######## 1) assigning value
meuse[1,'zinc'] <- NA
# Error in meuse[1, "zinc"] <- NA : object of type 'S4' is not subsettable
as.data.frame(meuse)[1,'zinc'] <- NA
# Error in as.data.frame(meuse)[1, "zinc"] <- NA :
# could not find function "as.data.frame<-"
######## 2) operating with values
meuse[, 'zinc'] + 2
# Error in meuse[, "zinc"] + 2 : non-numeric argument to binary operator
对于这两种情况,我都发现了非常丑陋的解决方法:
# ad 1)
meuse2 <- as.data.frame(meuse)
meuse2[1, 'zinc'] <- NA
meuse2 <- SpatialPointsDataFrame(SpatialPoints(meuse), meuse2)
# ad 2)
as.data.frame(meuse)[, 'zinc'] + 2
但这些只是初学者的尝试,太丑陋和复杂......在R中一定要容易得多!