Thiago,

Are you sure? It works well in this example:

library(raster)
r <- raster(ncol=100, nrow=100)
r1 <- crop(r, extent(-10, 11, -10, 11))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(9, 30, 9, 30))

r1[] <- 1:ncell(r1)
r2[] <- 1:ncell(r2)
r3[] <- 1:ncell(r3)

m1 <- merge(r1, r2, r3, fun=mean)
m2 <- merge(r3, r2, r1, fun=mean)
plot(stack(m1, m2))


If your case (small rasters) a work-around (that ignores the possibility of
NA values in r2) would be

cells <- cellFromXY(r1,  xyFromCell(r2, 1:ncell(r2)))
r1[cells] <- values(r2)

Robert


On Tue, Jun 19, 2012 at 9:20 AM, Thiago Veloso <thi_veloso@yahoo.com.br>wrote:

>  Dear list,
>
>  I have two rasters (r1 and r2). r1 has larger extent; r2 is smaller and
> its extents fall totally within r1. Here are the rasters:
>
> > r1
>
> class       : RasterLayer
> dimensions  : 360, 720, 259200  (nrow, ncol, ncell)
> resolution  : 0.5, 0.5  (x, y)
> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> values      : in memory
> layer name  : layer
> min value   : 0
> max value   : 0.055233
>
> > r2
>
> class       : RasterLayer
> dimensions  : 47, 65, 3055  (nrow, ncol, ncell)
> resolution  : 0.5, 0.5  (x, y)
> extent      : -74.5, -42, -18, 5.5  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> values      : in memory
> layer name  : layer
> min value   : 0
> max value   : 1
>
>  What I would like to be able to do is merge both rasters, but keeping r2
> values when cells overlap. In other words, replace r1 values with r2 values
> only when cells overlap. Following older discussions, I have tried "merge"
> command without sucess. Neither "r3 <- merge(r1,r2)" nor "r3 <-
> merge(r2,r1)" yield what I pursue.
>
>  Any ideas?
>
>  Many thanks in advance,
>
>  Thiago.
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

