在 R 编程中比较两个对象的相等性——相同()函数
R语言中的相同()函数用于在两个对象相等时返回TRUE,否则返回FALSE。
Syntax: identical(a, b)
Parameters:
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))
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)
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] 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