Camille,

You can do:

library(raster)
# some data
r <- r1 <- r2 <- raster(nr=10, nc=10)
r1[] <- runif(ncell(r1))
r2[] <- runif(ncell(r2))

# I would try this, should work well
# for a relatively or a small data set
v <- na.omit(getValues(r2))
r[] <- apply(as.matrix(getValues(r1)), 1, function(x) min(abs(x-v)))
# as.matrix(r)
# plot(r)

# However, for a large data set, if the above
# complains about insuffucient memory, perhaps this:
# see the vignette about writing functions for large raster files
# http://cran.r-project.org/web/packages/raster/vignettes/functions.pdf

tr <- blockSize(r)
out <- writeStart(r, filename='test.tif', overwrite=TRUE)
for (i in 1:tr$n) {
z <- as.matrix(getValues(r1, tr$row[i], tr$nrows[i]))
x <- rep(NA, length(z))
for (j in 1:tr$n) {
v <- na.omit(getValues(r2, tr$row[j], tr$nrows[j]))
x <- pmin(apply(z, 1, function(x) min(abs(x-v))), x, na.rm=TRUE)
}
out <- writeValues(out, x, tr$row[i])
}
out <- writeStop(out)

# plot(out)



Best, Robert

On Thu, Mar 1, 2012 at 12:28 AM, Camille Leclerc
<camille.leclerc@ymail.com>wrote:

> Hi everyone,
>
> I have two Raster layers : the first is the current global annual
> temperature (cgat) and the second is the future global annual temperature
> (fgat).
> First, I extracted the values of the area that interested me for the two
> raster layers (extract_cgat and extract fgat).
> And after, I calculated the value of one distance metric between the
> current and the future climate realizations for each gridpoint :
> “distance<-overlay(extract_cgat, extract_fgat, fun=function(x,y){formula})”
> and I obtained the Raster layer wanted.
>
> But now, I want to calculate the value of distance metric between one
> gridpoint of extract_fgat raster and all gridpoint of extract_cgat raster
> and keep the min value. And finally, remake this step for all gridpoint of
> extract_fgat raster and obtain the Raster layers with all “min value
> gridpoint”. I have no idea how I will make this, any ideas?
>
> Thank you very much for all help,
> Camille Leclerc
> --
> Lab ESE, UMR CNRS 8079
> Univ Paris-Sud
> Bat 362
> F-91405  Orsay Cedex FRANCE
> e-mail: camille.leclerc@ymail.com
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

	[[alternative HTML version deleted]]

