Skip to content
Prev 18186 / 29559 Next

Mapping cells from dataframe constructed in raster

Hi Janice,

That works but seems much more complicated than need be. I think you
should read the vignette that comes with the raster package:
http://cran.r-project.org/web/packages/raster/vignettes/Raster.pdf

Here is a simpler approach

library(raster)
s <- stack(system.file("external/rlogo.grd", package="raster"))
# create a matrix
m <- as.matrix(s)

# add a column with the cell numbers
m <- cbind(cell=1:ncell(s), m)

# it seems that in your case this is not needed because you used
# write.csv (which adds the cellsnumbers (rows in matrix) and then
read.csv to accomplish
# the same

# for this example, I take a random sample of 100 cells
set.seed(0)
x <- sample(m[,1], 100)

# create a RasterLayer template from the RasterStack
r <- raster(s)

# assign values to cells 'x'
r[x] <- 1:length(x)
plot(r)


Next time you ask a question provide a self-contained example like
Andrew's or  this one, which makes it much easier to answer.

Robert
On Thu, May 2, 2013 at 9:20 AM, Janice Lee <janice.jlsh at gmail.com> wrote: