[R-sig-Geo] daily precipitation idw & kriging and beginnerquestions

Tom Gottfried tom.gottfried at wzw.tum.de
Mon Aug 24 18:29:43 CEST 2009


Hi Ivan,

Soenario, Ivan (KNMI) schrieb:
> I've got the interpolations
> working, the printing as well, only one detail left:
> The dev.copy action results in overwriting the same file with the last
> component in list P_idw.
> 
> So I tried:
> dummy <- mapply(foo.plotting, jan2000.idw, jan2000.kri)
> 
> foo.plotting <- function(P_idw, P_kri) {
> 	i <- i+1
> 	P_idw$idw = P_idw$var1.pred
> 	P_idw$ok = P_kri$krige_output$var1.pred
> 	print(spplot(P_idw, c("idw", "ok")))
> 	dev.copy(jpeg, file = paste( wordir, "/img/Pd_idwkri_",
> 20000100+i, ".jpg", sep = ""), height = 17, width = 17, units = "cm",
> quality = 100, res=150, bg="white"); dev.off()
> }
> 
> But the 'i' won't increase. 

This lies in nature of all the functions of the "apply-family". They do not loop over the list
elements but apply the given function to list elements somehow in parallel.

> I could go back to a for-loop, but I find
> lapply and mapply too elegant to do that. So I have to use something
> else instead of a counter 'i'.

A further argument to foo.plotting(), giving the filename or something else which is useful in your
call to paste() for example.

> Since P_idw is a list containing the interpolated maps, is there a
> method to retrieve the list element, that is currently being processed
> by mapply? I can use that for the main title and the image file name.

As mapply() actually retrieves each element of the lists given as it's "..." argument and applies
FUN to them, I assume you intend to retrieve the name of the list element. As far as I know this is
impossible from within FUN, but you can retrieve the names before with names() and pass this as an
argument to FUN. Something like

foo.plotting <- function(P_idw, P_kri, names(P_idw)){
	...
	}

> Ps2: what does 'foo' stand for? Function-of-your-own??

Thanks to your hint I found that: http://en.wikipedia.org/wiki/Foobar. Function-of-your-own is not
mentioned but it's at least a good idea which might be added ;-)

regards,
Tom



More information about the R-sig-Geo mailing list