當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 identical()用法及代碼示例


R 語言中的 identical() 函數用於當兩個對象相等時返回 TRUE,否則返回 FALSE。

用法: identical(a, b)
參數: 
a, b: specified two objects 
 

範例1:

Python3


# R program to illustrate
# identical function
  
# Calling the identical() function
identical(factorial(3), gamma(4))
identical(lfactorial(5), log(factorial(5)))
identical(exp(2) - 1, expm1(2))

輸出:



[1] TRUE
[1] TRUE
[1] TRUE

範例2:

Python3


# R program to illustrate
# identical function
  
# Calling the identical() function
identical(.99, 1)
identical(1, 1)
identical(0, 0 / 2)
identical(5, 25 / 5)
identical(2, "2")
identical(T, TRUE)
identical(1, TRUE)
identical(F, FALSE)
identical(0, FALSE)
identical(1 / 0, Inf)
identical(0, -0)
identical(NaN, -NaN)

輸出:

[1] FALSE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] TRUE

範例3:

Python3


# R program to illustrate
# identical function
  
# Elements are arranged sequentially by row.
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)
 
# Elements are arranged sequentially by column.
N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(N)
 
# Calling identical() function
identical(M, N)

輸出:

     [, 1] [, 2] [, 3]
[1, ]    3    4    5
[2, ]    6    7    8
[3, ]    9   10   11
[4, ]   12   13   14
     [, 1] [, 2] [, 3]
[1, ]    3    7   11
[2, ]    4    8   12
[3, ]    5    9   13
[4, ]    6   10   14
[1] FALSE




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Compare two Objects for Equality in R Programming – identical() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。