Hi Robert,

Thanks for your answer! Greatly appreciated.

Ok, will do so the next time.

Regards,
Janice




On Fri, May 3, 2013 at 10:25 PM, Robert J. Hijmans <r.hijmans@gmail.com>wrote:

> 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@gmail.com> wrote:
> > Dear all,
> >
> > I would appreciate anyone's help on this.
> > I constructed a dataframe from several raster layers and compiled these
> > layers using the 'stack' and 'brick' function in 'raster' package
> >
> > s <- stack(r1,r2,r3,r4,r5)
> > b <- brick(s)
> >
> > I converted this into a matrix and stored this as a dataframe:
> >
> > mb <- as.matrix(b)
> > write.csv(mb,"C:/Source/mb.csv")
> >
> > This new dataframe (mb) lists all the cells as rows and all the
> attributes
> > in my raster layers (r1-r5) as columns
> > The first column in mb also provides a number to each cell which I assume
> > acts as a form of identification number to every grid cell
> > I used mb in an R-coded model which simulates land cover change, in
> which I
> > selected the most suitable cells for land use conversion
> > My final dataframe (df) has a lower number of rows compared to mb but has
> > preserved the identification number of each grid cell.
> >
> > My question is: Can I map out the cells in df by tracing its
> identification
> > number back to the original dataframe mb?
> > I have been trying to figure this out and will appreciate any
> suggestions.
> >
> > Many thanks and best regards,
> > Janice Lee
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
_______________________________________

*Janice Lee Ser Huay*
PhD candidate
Applied Ecology & Conservation
Institute of Terrestrial Ecosystems
ETH Zurich | Switzerland

	[[alternative HTML version deleted]]

