[R] allequal diff

Ben Bolker bbo|ker @end|ng |rom gm@||@com
Sun Aug 18 20:44:42 CEST 2024


    The OP's original problem is that the all.equal method for raster 
objects (raster:::all.equal.raster), which is a wrapper around the 
compareRaster() function, compares a bunch of different properties of 
rasters (extent, resolution, values, etc.) and only returns a single 
overall logical (TRUE/FALSE) value. OP wanted to see the magnitude of 
the difference (as you could get for more typical all.equal methods by 
using tolerance=0), but in order to do that one has to dig into the code 
of compareRaster() and pull out code to make the particular comparisons 
one wants by applying all.equal to specific components of the raster (it 
would be nice if there were a built-in way to get this information, but 
I don't know of one)

On 8/18/24 14:40, Bert Gunter wrote:
> "Is it true that all.equal just compares y values?"
>
> The following may be a bit more than you may have wanted, but I hope
> it is nevertheless useful.
>
> The first place you should go to for questions like this is the Help
> system, not here, i.e.
> ?all.equal
>
> When you do this, you will find that all.equal() is a so-called S3
> generic function, which, among other things, means that it works
> differently (i.e. "dispatches") depending on its (usually) first
> argument. So, for example, if the first argument is of (S3) class
> "numeric", it will call the default method, all.equal.default(); if
> it's a function, it will call all.equal.function().  Help for
> all.equal's basic methods is found in the single all.equal (base) Help
> page. However, for non-base R packages, there may be other different
> methods provided for classed objects, e.g. perhaps of class "raster"
> that would be found by ?all.equal.raster . Or maybe not, if the class
> "inherits" from another class, such as "matrix" (Warning: I am
> completely unfamiliar with the raster package, so these specifics are
> very likely wrong).
>
> To sort this sort of thing out, It would probably be useful for you to
> find a tutorial on R's S3 class  system (which is really a form of
> multiple dispatch) and spend some time with it. There are many good
> ones out there. This S3 system is widely used within R and many
> packages, so doing this sort of homework now should serve you well in
> your future R journey.
>
> All IMO obviously.
>
> Cheers,
> Bert
>
>
> On Sun, Aug 18, 2024 at 11:00 AM SIBYLLE STÖCKLI via R-help
> <r-help using r-project.org> wrote:
>> Dear Ivan
>>
>> Thanks a lot for this very nice example.
>>
>> Is it true that all.equal just compares y values?
>> Based on this help here I think so and the value I got is the difference for the y-values.
>> https://www.statology.org/all-equal-function-r/
>>
>> However, here I see x and y testing?
>> https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/all.equal
>> I am actually interested in the x values (x-y coordinates). Test if x-y coordinates of both 25-m-pixel rasters are the same. Ther may be a small shift or differences in the number of decimal places.
>>
>> Kind regards
>> Sibylle
>>
>>
>>
>> -----Original Message-----
>> From: Ivan Krylov <ikrylov using disroot.org>
>> Sent: Friday, August 16, 2024 11:45 AM
>> To: sibylle.stoeckli using gmx.ch
>> Cc: 'SIBYLLE STÖCKLI via R-help' <r-help using r-project.org>
>> Subject: Re: [R] allequal diff
>>
>> В Fri, 16 Aug 2024 11:32:58 +0200
>> <sibylle.stoeckli using gmx.ch> пишет:
>>
>>> # values and mask r1
>>> r1 <- getValues(r1)
>>> mask1 <- is.na(r1)
>>> # Do the same for r2
>>> r2 <- getValues(r2_resampled)
>>> mask2 <- is.na(r2)
>>>
>>> # Combine the masks
>>> all.equal(r1[!(mask1 & mask2)], r2[!(mask1 & mask2)])
>> Let's consider a more tangible example:
>>
>> # The vectors `x` and `y` start out equal x <- y <- 1:10 # But then their different elements are made missing x[c(1,3,4)] <- NA y[c(3,8)] <- NA
>>
>> Now, `is.na(x) & is.na(y)` gives the third element as the only element missing in both x and y:
>>
>> mask1 <- is.na(x)
>> mask2 <- is.na(y)
>> all.equal( # not the comparison you are looking for
>>   x[!(mask1 & mask2)], # still two more elements missing
>>   y[!(mask1 & mask2)]  # still one more element missing
>> )
>>
>> If you want to ignore all missing elements, you should combine the masks using the element-wise "or" operation ("missing in x and/or y"), not the element-wise "and" operation ("missing in both x and y at the same time"):
>>
>> mask1 & mask2 # drops element 3
>> # [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>> mask1 | mask2 # drops elements 1, 3, 4, 8 # [1]  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE
>>
>> --
>> Best regards,
>> Ivan
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list