r - Assign and use data values of Spatial*DataFrame easily -
is possible somehow work data in spatial*dataframe (* = points, lines, polygons, pixels, grid,...)? in particular have difficulties assigning values , operate 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' not subsettable as.data.frame(meuse)[1,'zinc'] <- na # error in as.data.frame(meuse)[1, "zinc"] <- na :  #   not find function "as.data.frame<-"  ######## 2) operating values  meuse[, 'zinc'] + 2 # error in meuse[, "zinc"] + 2 : non-numeric argument binary operator   i have found pretty ugly workarounds 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 beginners' attempts, way ugly , complicated... must easier in r!
for spatial*dataframe objects, can access data.frame slot '@data', , usual data.frame operations should work. using example,
 meuse@data[1, 'zinc'] <- na   gives
 str(meuse@data) 'data.frame':   155 obs. of  12 variables:  $ cadmium: num  11.7 8.6 6.5 2.6 2.8 3 3.2 2.8 2.4 1.6 ...  $ copper : num  85 81 68 81 48 61 31 29 37 24 ...  $ lead   : num  299 277 199 116 117 137 132 150 133 80 ...  $ zinc   : num  na 1141 640 257 269 ...  ...      
Comments
Post a Comment