all.equal(x, y) adalah utilitas untuk membandingkan R objek x dan y yang menguji 'mendekati kesetaraan'. Jika berbeda, perbandingan tetap dilakukan sampai batas tertentu, dan laporan perbedaannya dikembalikan. Jangan gunakan all.equal secara langsung dalam ekspresi if---gunakan isTRUE(all.equal(....)) atau identik jika sesuai.
all.equal(target, current, …)<p></p><p># S3 method for numeric
all.equal(target, current,
tolerance = sqrt(.Machine$double.eps), scale = NULL,
countEQ = FALSE,
formatFUN = function(err, what) format(err),
…, check.attributes = TRUE)</p><p># S3 method for list
all.equal(target, current, …,
check.attributes = TRUE, use.names = TRUE)</p><p># S3 method for environment
all.equal(target, current, all.names=TRUE, …)</p><p># S3 method for POSIXt
all.equal(target, current, …, tolerance = 1e-3, scale)</p><p>
attr.all.equal(target, current, …,
check.attributes = TRUE, check.names = TRUE)</p>
| Parameter | Deskripsi |
|---|---|
target |
R object. |
current |
other R object, to be compared with target. |
… |
further arguments for different methods, notably the following two, for numerical comparison: |
tolerance |
numeric \(\ge\) 0. Differences smaller than tolerance are not reported. The default value is close to 1.5e-8. |
scale |
NULL or numeric > 0, typically of length 1 or length(target). See ‘Details’. |
countEQ |
logical indicating if the target == current cases should be counted when computing the mean (absolute or relative) differences. The default, FALSE may seem misleading in cases where target and current only differ in a few places; see the extensive example. |
formatFUN |
a function of two arguments, err, the relative, absolute or scaled error, and what, a character string indicating the kind of error; maybe used, e.g., to format relative and absolute errors differently. |
check.attributes |
logical indicating if the attributes of target and current (other than the names) should be compared. |
use.names |
logical indicating if list comparison should report differing components by name (if matching) instead of integer index. Note that this comes after … and so must be specified by its full name. |
all.names |
logical passed to ls indicating if “hidden” objects should also be considered in the environments. |
check.names |
logical indicating if the names(.) of target and current should be compared. |
# NOT RUN {
all.equal(pi, 355/113)
# not precise enough (default tol) > relative error
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
all.equal(tan(d45), rep(1, 10), tolerance = 0) # to see difference
## advanced: equality of environments
ae <- all.equal(as.environment("package:stats"),
asNamespace("stats"))
stopifnot(is.character(ae), length(ae) > 10,
## were incorrectly "considered equal" in R <= 3.1.1
all.equal(asNamespace("stats"), asNamespace("stats")))
## A situation where 'countEQ = TRUE' makes sense:
x1 <- x2 <- (1:100)/10; x2[2] <- 1.1*x1[2]
## 99 out of 100 pairs (x1[i], x2[i]) are equal:
plot(x1,x2, main = "all.equal.numeric() -- not counting equal parts")
all.equal(x1,x2) ## "Mean relative difference: 0.1"
mtext(paste("all.equal(x1,x2) :", all.equal(x1,x2)), line= -2)
##' extract the 'Mean relative difference' as number:
all.eqNum <- function(...) as.numeric(sub(".*:", '', all.equal(...)))
set.seed(17)
## When x2 is jittered, typically all pairs (x1[i],x2[i]) do differ:
summary(r <- replicate(100, all.eqNum(x1, x2*(1+rnorm(x1)*1e-7))))
mtext(paste("mean(all.equal(x1, x2*(1 + eps_k))) {100 x} Mean rel.diff.=",
signif(mean(r), 3)), line = -4, adj=0)
## With argument countEQ=TRUE, get "the same" (w/o need for jittering):
mtext(paste("all.equal(x1,x2, countEQ=TRUE) :",
signif(all.eqNum(x1,x2, countEQ=TRUE), 3)), line= -6, col=2)
# }