当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。