📜  在 R 编程中比较数据帧的值 - all_equal()函数

📅  最后修改于: 2022-05-13 01:54:26.320000             🧑  作者: Mango

在 R 编程中比较数据帧的值 - all_equal()函数

R语言中的all_equal()函数 用于比较数据帧之间的值。

R – 比较数据帧的值

示例 1:比较相等的数据帧

R
# R program to illustrate
# all_equal function
 
# Create three data frames
data1 <- data.frame(x1 = 1:10,           
                    x2 = LETTERS[1:10])
data2 <- data.frame(x1 = 1:10,
                    x2 = LETTERS[1:10])
data3 <- data.frame(x1 = 2:12,
                    x2 = LETTERS[1:5])
 
# Compare equal data frames
all_equal(data1, data2, check.attributes = FALSE)


R
# R program to illustrate
# all_equal function
 
# Create three data frames
data1 <- data.frame(x1 = 1:10,           
                    x2 = LETTERS[1:10])
data2 <- data.frame(x1 = 1:10,
                    x2 = LETTERS[1:10])
data3 <- data.frame(x1 = 2:12,
                    x2 = LETTERS[1:5])
 
# Compare unequal data frames
all_equal(data1, data3, check.names = FALSE)


输出:

TRUE

在上面的代码中,我们比较了相等的数据帧 data1 和 data2,因此输出似乎是“TRUE”。

示例 2:比较不相等的数据帧

R

# R program to illustrate
# all_equal function
 
# Create three data frames
data1 <- data.frame(x1 = 1:10,           
                    x2 = LETTERS[1:10])
data2 <- data.frame(x1 = 1:10,
                    x2 = LETTERS[1:10])
data3 <- data.frame(x1 = 2:12,
                    x2 = LETTERS[1:5])
 
# Compare unequal data frames
all_equal(data1, data3, check.names = FALSE)                   

输出:

Rows in x but not y: 1, 2, 3, 4, 5, 6, 8, 9, 10. 
Rows in y but not x: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 

在上面的代码中,我们比较了不相等的数据帧 data1 和 data3,所以我们得到一个错误,解释代码中的错误。