all.equal
in R tests for differences (absolute / relative) for observations where mismatch happen, and then makes sure that it is within tolerance amount. Ideally, it should test all observations where mismatch happens by tolerance amount, and then report the differences... Why is the behavior so ?
eg In the following case, I would expect the result of all.equal to be FALSE, as the first observation in x is not equal to y
> x = rep(1, 1000)
> y = rep(1, 1000)
> x = x + 0.001
> y[1] = 2
> print(all.equal(x, y, scale = 1, tolerance=0.01))
[1] TRUE
The all.equal
function returns TRUE if the the target and current arguments are nearly equal up to a specified tolerance, which has a default value close to 1.5e-8. The function does not return FALSE otherwise, rather it returns the mean relative ( scale=NULL
) or absolute ( scale=1
) difference.
x = rep(1, 1000)
y = rep(1, 1000)
x = x + 0.001
y[1] = 2
print(all.equal(x, y, scale = 1)) # Omit the tolerance
[1] "Mean absolute difference: 0.001998"
mean(abs(x-y))
[1] 0.001998
If you read the help page for all.equal, you would see the reason:
tolerance numeric ≥ 0. Differences smaller than tolerance are not reported.
Edit : Based on Aaron's assessment of the OP's source of confusion, perhaps the any
function was required:
y[1] = 1
any(abs(x-y) > 0.01) # Element-wise comparison
[1] FALSE
y[1] = 2
any(abs(x-y) > 0.01)
[1] TRUE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.