突出显示 R 中 2 个数据帧之间的差异

问题描述 投票:0回答:1

有什么方法可以比较R中的2个数据帧

我想要一个视觉比较,我可以看到两个数据帧并且突出显示差异,类似于我比较两个段落时

我比较的目的是可能有一些值不同或缺失,我应该得到的结果中有额外的行。

r dataframe comparison
1个回答
0
投票

您可以使用 compareDF 包。要比较的两个数据帧必须具有相同的列结构。

library(compareDF)

df1 <- iris[1:5, ]
df1$Species <- as.character(df1$Species) # "unfactor"
df2 <- df1
df2[1, 1] <-10
df2[2, 5] <- "tulip"

# create comparison object
comp <- compare_df(df1, df2)

# visualize it in a "git diff" style
create_output_table(comp)

# wide format
create_wide_output(comp)
#   rowname Species_old Species_new Sepal.Width_old Sepal.Width_new
# 1       1      setosa      setosa             3.5             3.5
# 2       2       tulip      setosa             3.0             3.0
#   Sepal.Length_old Sepal.Length_new Petal.Width_old Petal.Width_new
# 1             10.0              5.1             0.2             0.2
# 2              4.9              4.9             0.2             0.2
#   Petal.Length_old Petal.Length_new
# 1              1.4              1.4
# 2              1.4              1.4
© www.soinside.com 2019 - 2024. All rights reserved.