Ani,

Here are some alternatives approaches.

# your data
m1<-matrix(c(1,1,1,2,2,2,3,3,3),ncol=3,byrow=TRUE)
m2<-matrix(c(1,2,1,1,2,3,2,3,3),ncol=3,byrow=TRUE)
r1<-raster(m1)
r2<-raster(m2)

# I think this is the most elegant approach for mutli-layer
reclassification problem.
# caveat: it may be slow. Also note that all other combinations become NA
(not zero)
#  but perhaps it is better that way

d <- data.frame(matrix(c(1,1,1,1,2,2), ncol=3, byrow=TRUE))
s <- subs( stack(r1, r2), d, by=1:2, which=3)


# and then there is overlay and calc. They do the same as what Etienne
showed, but these will be faster for large files
# and memory safe and you could add a filename=   argument.
# here is use 0 for a background value, but you could replace that with NA

b <- overlay(r1, r2, fun=function(x,y) {z=x; z[]=0; z[x==1&y==1]=1;
z[x==1&y==2]=2; z} )

e <- calc(stack(r1, r2), fun=function(x) {z=x[,1]; z[] = 0;
z[x[,1]==1&x[,2]==1]=1; z[x[,1]==1&x[,2]==2]=2; z} )

Robert


On Thu, Oct 18, 2012 at 6:06 AM, aniruddha ghosh <aniru123@gmail.com> wrote:

> Thank you Etienne and Steven for your suggestions.
>
> I tried the ordinary matrix method as suggested by Etienne. It works
> fine. It was fast and produced excellent results for my original
> raster files with 10 values.
>
> Steven, when I try to incorporate more number of cell values, the
> procedure is getting longer as I have to reclassify multiple times for
> different combinations.
>
> Best regards,
>
>
> On Thu, Oct 18, 2012 at 1:50 AM, steven mosher <moshersteven@gmail.com>
> wrote:
> >
> > reclassify r1 to 1 and 0. reclassify r2 to 0 1 and 2.
> > then multiply the rasters.
> > so r1 becomes 0 for all values ecept 1. r2 is 1 or 2 or 0. and the
> product r3 will be 1 or 2 or 0
> >
> > On Oct 17, 2012 11:55 AM, "aniruddha ghosh" <aniru123@gmail.com> wrote:
> >>
> >> Dear List,
> >> I have two raster layers (r1 and r2) and the cells have values ranging
> 1 to
> >> 10. I want to create a new raster layer from these two with different
> >> conditions like:
> >> 1) if r1==1 and r2==1, then r3 should be 1
> >> 2) if r1==1 and r2==2, then r3 should be 2
> >> else 0 etc
> >>
> >> With the following example:
> >>
> >> m1<-matrix(c(1,1,1,2,2,2,3,3,3),ncol=3,byrow=TRUE)
> >> m2<-matrix(c(1,2,1,1,2,3,2,3,3),ncol=3,byrow=TRUE)
> >> r1<-raster(m1)
> >> r2<-raster(m2)
> >> Here, r1[1,1]=1 and r2[1,1]=1, so for the new raster, [1,1] should be 1,
> >> r1[1,2]=1 and r2[1,2]=2, so for the new raster, [1,2] should be 2,
> >> all other cells which don't satisfy the above criteria should be zero.
> >>
> >> I tried with "calc" in raster package but the attempt was not
> successful.
> >>
> >> Looking forward to your suggestions!
> >>
> >>
> >> Thanks
> >> --
> >> Ani
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >> _______________________________________________
> >> R-sig-Geo mailing list
> >> R-sig-Geo@r-project.org
> >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
>
> --
> Ani
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

