Is it possible to somehow easily work with data in Spatial*DataFrame
(* = Points, Lines, Polygons, Pixels, Grid,...)? In particular I have difficulties with assigning values to it and to operate with them:
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
I have found pretty ugly workarounds for both cases:
# ad 1)
meuse2 <- as.data.frame(meuse)
meuse2[1, 'zinc'] <- NA
meuse2 <- SpatialPointsDataFrame(SpatialPoints(meuse), meuse2)
# ad 2)
as.data.frame(meuse)[, 'zinc'] + 2
but these are just beginners' attempts, way too ugly and complicated... Must be much easier in R!
For Spatial*DataFrame objects, you can access the data.frame slot with '@data', and the usual data.frame operations should work. Using your example,
gives