[R] allequal diff

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Fri Aug 16 11:45:24 CEST 2024


В 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