比较r中两个数据帧之间的差异

发布于 2025-02-08 22:37:49 字数 1073 浏览 1 评论 0 原文

我正在遵循,但R代码似乎不起作用(没有结果),我不确定为什么。有人可以帮助解释我在这里做错了什么吗?

df1 <- data.frame(id=c("632592651","633322173","634703802","634927873","635812953","636004739","636101211","636157799","636263106","636752420"),
        text=c("asdf","cat","dog","mouse","elephant","goose","rat","mice","kitty","kitten"))

df2 <- data.frame(id=c("632592651","633322173","634703802","634927873","635812953","636004739","636101211","636157799","636263106","636752420","636809222","2004722036","2004894388","2005045755","2005535472","2005630542","2005788781","2005809679","2005838317","2005866692"),
        text=c("asdf_xyz","cat","dog","mouse","elephant","goose","rat","mice","kitty","kitten","tiger_xyz","lion","leopard","ostrich","kangaroo","platypus","fish","reptile","mammals","amphibians_xyz"))

diff1 <- df1[setdiff(df1$refid, df2$refid),]

diff2 <- df2[!(intersect(df2$refid, df1$refid)),]

I'm following the solution provided in Determining different rows between two data sets in R but the R code doesn't seem to work (getting no results) and I'm not sure why. Can someone help to explain what I have done wrong here?

df1 <- data.frame(id=c("632592651","633322173","634703802","634927873","635812953","636004739","636101211","636157799","636263106","636752420"),
        text=c("asdf","cat","dog","mouse","elephant","goose","rat","mice","kitty","kitten"))

df2 <- data.frame(id=c("632592651","633322173","634703802","634927873","635812953","636004739","636101211","636157799","636263106","636752420","636809222","2004722036","2004894388","2005045755","2005535472","2005630542","2005788781","2005809679","2005838317","2005866692"),
        text=c("asdf_xyz","cat","dog","mouse","elephant","goose","rat","mice","kitty","kitten","tiger_xyz","lion","leopard","ostrich","kangaroo","platypus","fish","reptile","mammals","amphibians_xyz"))

diff1 <- df1[setdiff(df1$refid, df2$refid),]

diff2 <- df2[!(intersect(df2$refid, df1$refid)),]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-02-15 22:37:49
library(tidyverse)

anti_join(df1,df2)

Joining, by = c("id", "text")
         id text
1 632592651 asdf



anti_join(df2,df1)

Joining, by = c("id", "text")
           id           text
1   632592651       asdf_xyz
2   636809222      tiger_xyz
3  2004722036           lion
4  2004894388        leopard
5  2005045755        ostrich
6  2005535472       kangaroo
7  2005630542       platypus
8  2005788781           fish
9  2005809679        reptile
10 2005838317        mammals
11 2005866692 amphibians_xyz
library(tidyverse)

anti_join(df1,df2)

Joining, by = c("id", "text")
         id text
1 632592651 asdf



anti_join(df2,df1)

Joining, by = c("id", "text")
           id           text
1   632592651       asdf_xyz
2   636809222      tiger_xyz
3  2004722036           lion
4  2004894388        leopard
5  2005045755        ostrich
6  2005535472       kangaroo
7  2005630542       platypus
8  2005788781           fish
9  2005809679        reptile
10 2005838317        mammals
11 2005866692 amphibians_xyz
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文