# Adding values to a raster Classic List Threaded 2 messages Reply | Threaded
Open this post in threaded view
|

## Adding values to a raster

 I have a dataset that consists of point coordinates and values associated with the points.  I want to be able to grid the values, and then do calulations using the values using the various raster functions.  I am, however, finding this difficult to do.  I have tried a number of functions including rasterize, SpatialGridDataFrame, and SpatialGridDataFrame but I think I am missing a bit of the theory behind this that I can't seem to piece together. I have the Applied Spatial Data Analysis (Bivand) but couldn't seem to find an appropriate section.  Here is an example data set: require(raster) r <- raster(ncol=10, nrow=10) r[]=1:ncell(r) x <- c(-45, 30, 20, -10, 50, -7) y <- c(-25, -148, 46, 23, 0, -69) value <- c(1, 2, 3, 4, 5, 6) coord.plus.value <- cbind(x, y, value) coord.plus.value_sp <- SpatialPoints(coord.plus.value) >From here I want to plot 'value' using the raster 'r'. Suggests on how to do this, as well as thoughts on the basic theory I seem to be missing (or where in the Bivand book I should be reading that I have missed) would be appreciated. Thank you, Tara Bridwell _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

## Re: Adding values to a raster

 Tara, I think you need to 'rasterize' function library(raster) r <- raster(ncol=10, nrow=10) x <- c(-25, -148, 46, 23, 0, -69) y <- c(-45, 30, 20, -10, 50, -7) value <- c(1, 2, 3, 4, 5, 6) # you can do this: r1 <- rasterize(cbind(x, y), r, value) plot(r1) # or sp <- SpatialPointsDataFrame(cbind(x,y), data.frame(v=value)) r2 <- rasterize(sp, r, 'v') # or a more "manual" approach cells <- cellFromXY(r, cbind(x,y)) r[cells] <- value # safer would be: # r[cells[!is.na(cells)]] <- value[!is.na(cells)] This is not in the Bivand et al book ( which was written before the raster package was released ). Robert On Sat, Sep 17, 2011 at 4:48 PM, Tara Bridwell <[hidden email]>wrote: > I have a dataset that consists of point coordinates and values > associated with the points.  I want to be able to grid the values, and > then do calulations using the values using the various raster > functions.  I am, however, finding this difficult to do.  I have tried > a number of functions including rasterize, SpatialGridDataFrame, and > SpatialGridDataFrame but I think I am missing a bit of the theory > behind this that I can't seem to piece together. I have the Applied > Spatial Data Analysis (Bivand) but couldn't seem to find an > appropriate section.  Here is an example data set: > > require(raster) > > r <- raster(ncol=10, nrow=10) > > r[]=1:ncell(r) > > x <- c(-45, 30, 20, -10, 50, -7) > > y <- c(-25, -148, 46, 23, 0, -69) > > value <- c(1, 2, 3, 4, 5, 6) > > coord.plus.value <- cbind(x, y, value) > > coord.plus.value_sp <- SpatialPoints(coord.plus.value) > > >From here I want to plot 'value' using the raster 'r'. > > Suggests on how to do this, as well as thoughts on the basic theory I > seem to be missing (or where in the Bivand book I should be reading > that I have missed) would be appreciated. > > Thank you, > > Tara Bridwell > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo>         [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo