ID EN
Comparison & Logic

identical

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Cara yang aman dan andal untuk menguji dua objek agar sama persis. Ia mengembalikan TRUE dalam kasus ini, FALSE dalam kasus lainnya.

Syntax

R
identical(x, y, num.eq = TRUE, single.NA = TRUE, attrib.as.set = TRUE,
          ignore.bytecode = TRUE, ignore.environment = FALSE,
          ignore.srcref = TRUE)

Arguments

Parameter Deskripsi
x, y any R objects.
num.eq logical indicating if (double and complex non-NA) numbers should be compared using == (‘equal’), or by bitwise comparison. The latter (non-default) differentiates between -0 and +0.
single.NA logical indicating if there is conceptually just one numeric NA and one NaN; single.NA = FALSE differentiates bit patterns.
attrib.as.set logical indicating if attributes of x and y should be treated as unordered tagged pairlists (“sets”); this currently also applies to slots of S4 objects. It may well be too strict to set attrib.as.set = FALSE.
ignore.bytecode logical indicating if byte code should be ignored when comparing closures.
ignore.environment logical indicating if their environments should be ignored when comparing closures.
ignore.srcref logical indicating if their "srcref" attributes should be ignored when comparing closures.

Return Value

Nilai logis tunggal, TRUE atau FALSE, tidak pernah NA dan tidak pernah apa pun selain nilai tunggal.

Details

Panggilan ke identik adalah cara untuk menguji kesetaraan yang tepat dalam pernyataan if dan while, serta dalam ekspresi logis yang menggunakan && atau ||. Dalam semua aplikasi ini Anda perlu yakin untuk mendapatkan satu nilai logis. Pengguna sering menggunakan operator perbandingan, seperti == atau !=, dalam situasi ini. Kelihatannya wajar, tapi bukan itu yang dirancang untuk dilakukan oleh operator ini di R. Mereka mengembalikan objek seperti argumennya. Jika Anda mengharapkan x dan y memiliki panjang 1, tetapi ternyata salah satunya tidak, Anda tidak akan mendapatkan satu pun FALSE. Begitu pula jika salah satu argumennya NA, maka hasilnya juga NA. Dalam kedua kasus tersebut, ekspresi if(x == y).... tidak akan berfungsi seperti yang diharapkan. Fungsi all.equal juga kadang-kadang digunakan untuk menguji kesetaraan dengan cara ini, namun dimaksudkan untuk sesuatu yang berbeda: memungkinkan fo

Contoh

Example
R
# NOT RUN {
identical(1, NULL) ## FALSE -- don't try this with ==
identical(1, 1.)   ## TRUE in R (both are stored as doubles)
identical(1, as.integer(1)) ## FALSE, stored as different types

x <- 1.0; y <- 0.99999999999
## how to test for object equality allowing for numeric fuzz :
(E <- all.equal(x, y))
isTRUE(E) # which is simply defined to just use
identical(TRUE, E)
## If all.equal thinks the objects are different, it returns a
## character string, and the above expression evaluates to FALSE

## even for unusual R objects :
identical(.GlobalEnv, environment())

### ------- Pickyness Flags : -----------------------------

## the infamous example:
identical(0., -0.) # TRUE, i.e. not differentiated
identical(0., -0., num.eq = FALSE)
## similar:
identical(NaN, -NaN) # TRUE
identical(NaN, -NaN, single.NA = FALSE) # differ on bit-level

### For functions ("closure"s): ----------------------------------------------
###     ~~~~~~~~~
f <- function(x) x
f
g <- compiler::cmpfun(f)
g
identical(f, g)                        # TRUE, as bytecode is ignored by default
identical(f, g, ignore.bytecode=FALSE) # FALSE: bytecode differs

## GLM families contain several functions, some of which share an environment:
p1 <- poisson() ; p2 <- poisson()
identical(p1, p2)                          # FALSE
identical(p1, p2, ignore.environment=TRUE) # TRUE

## in interactive use, the 'keep.source' option is typically true:
op <- options(keep.source = TRUE) # and so, these have differing "srcref" :
f1 <- function() {}
f2 <- function() {}
identical(f1,f2)# ignore.srcref= TRUE : TRUE
identical(f1,f2,  ignore.srcref=FALSE)# FALSE
options(op) # revert to previous state

# }

See Also

all.equal for descriptions of how two objects differ; Comparison for operators that generate elementwise comparisons. isTRUE is a simple wrapper based on identical.