R语言 比较两个对象是否相等 – identical() 函数
R语言中的 identical() 函数是用来在两个对象相等时返回TRUE,否则返回FALSE。
语法: identical(a, b)
参数:
a, b: 指定两个对象
例子1 :
# 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 :
# 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 :
# 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