Janice,

I think my included code is a first stab at what you're trying to do.  I
hope this helps.

-Andrew

#=================================================================
library(raster)

#load in raster stack
st <- stack(system.file("external/rlogo.grd", package="raster"))

#make the stack a matrix or 3 columns, corresponding to nlayers(st)
m <- as.matrix(st)
#Add a 4th column with the cell numbers, as my m matrix does not contain any
#Are you sure what you have is cell numbers??
mc <- cbind(m, 1:ncell(st))

#create a new matrix with cells to delete some rows, as you state your
model does
new.mc <- mc
new.mc <- new.mc[-1:-10, ];new.mc <- new.mc[-1000:-1050, ]
new.mc <- new.mc[-6000:-7000,]

#create new raster layer from the extent of original data (st)
r <- raster(nrow = nrow(st), ncol = ncol(st))
projection(st) <- projection(r)
extent(st) <- extent(r)


#set the values all to NA for raster layer r
r[] <- NA

#create stack of NAs
s2 <- stack(r,r,r)
#Set the values.  Cell numbers are called from column 4 of matrix new.mc,
#cell values will be from column of new.mc corresponding to layer
s2[[1]][new.mc[,4]] <- new.mc[,1]
s2[[2]][new.mc[,4]] <- new.mc[,2]
s2[[3]][new.mc[,4]] <- new.mc[,3]

#Check that it worked
par(mfrow = c(1,2))
plotRGB(st, 1,2,3, stretch = 'hist')
plotRGB(s2, 1,2,3, stretch = 'hist')

#=================================================================


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
>



-- 
*Andrew P. Vitale*
Masters Student
Department of Geography
University of Nevada, Reno
(412) 915-3632
vitale232@gmail.com

	[[alternative HTML version deleted]]

