Digging into the code for raster::compareRaster():

library(raster)
r <- raster(ncol=3, nrow=3)
values(r) <- 1:ncell(r)
r2 <- r
values(r2) <- c(1:8,10)
all.equal(getValues(r), getValues(r2), tolerance = 0)
[1] "Mean relative difference: 0.1111111"

compareRaster has fancier machinery internally for doing the comparison for large rasters a block at a time if everything can't fit in memory at the same time ...




On 2024-08-15 9:14 a.m., SIBYLLE STÖCKLI via R-help wrote:
Dear community



Similar to the example of the rdocumentation, my idea is to use all.equal
and to print the difference.

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/all.equal



d45 <- pi*(1/4 + 1:10)

stopifnot(

+     all.equal(tan(d45), rep(1, 10)))          # TRUE, but

all      (tan(d45) == rep(1, 10))         # FALSE, since not exactly

[1] FALSE

all.equal(tan(d45), rep(1, 10), tolerance = 0)  # to see difference

[1] "Mean relative difference: 1.29526e-15"





Unfortunately, I just get "FALSE" not the difference.

r2_resampled <- resample(r2, r1)

compareRaster(r1, r2_resampled)

[1] TRUE

# Compare rasters

result <- all.equal(r1, r2_resampled)

Warning message:

In compareRaster(target, current, ..., values = values, stopiffalse =
stopiffalse,  :

   not all objects have the same values

print(result)

[1] FALSE





Kind regards

Sibylle


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
> E-mail is sent at my convenience; I don't expect replies outside of working hours.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to