Skip to content
Prev 7936 / 29559 Next

Specify merging mode

Etienne,

This is an interesting problem. I do not know what is going on, but
have insufficient time to study this right now.

This slightly adjusted code works in unexpected ways:

fmerge <- function(rasters, fun, ...){
	ex <- raster(unionExtent(rasters))	
	res(ex) <- res(rasters[[1]])
	for( i in 1:length(rasters) )
		rasters[[i]] <- merge(rasters[[i]], ex)
	fun(rasters[[1]], rasters[-1], na.rm=TRUE)  # to force the first
argument to be a RasterLayer object
}

rfmn <- fmerge(rasters, fun=mean, na.rm=T) ? #works
rfx <- fmerge(rasters, fun=max, na.rm=T) ? #wrong
fun = max
rfx <- fmerge(rasters, fun=max, na.rm=T) ? #works
rfn <- fmerge(rasters, fun=min, na.rm=T) ? #works, but still returns max !
fun = min
rfn <- fmerge(rasters, fun=min, na.rm=T) ? #works

So something or other that is different in the environment of the
function fmerge...  I suspect it has something to do with mean being a
normal generic while min and max are primitive functions and part of a
group-generic.


This is an alternative approach that I believe does work:

fmerge <- function(rasters, fun, ...){
	ex <- raster(unionExtent(rasters))
	res(ex) <- res(rasters[[1]])
	for( i in 1:length(rasters) )
	? 	rasters[[i]] <- merge(rasters[[i]], ex)
	rasters <- stack(rasters)
	fun(rasters, ...)
}

rfm <- fmerge(rasters, mean, na.rm=T) ?#ok
rfx <- fmerge(rasters, max, na.rm=T) ? #wrong


Also, I have now added a function 'mosaic' in raster (version 1.0.0-6)
that does this (merging with a function). mosaic() should make the
above functions redundant, but I would like to figure out what's going
on nevertheless. Ideas, anyone?

Robert







1.0.0-6

On Tue, Mar 30, 2010 at 8:32 AM, Etienne Bellemare Racine
<etiennebr at gmail.com> wrote: