[R] allequal diff

sibyiie@stoeckii m@iii@g oii gmx@ch sibyiie@stoeckii m@iii@g oii gmx@ch
Sun Aug 18 20:00:01 CEST 2024


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



More information about the R-help mailing list