显示文件或对象之间的差异

发布于 2024-12-10 21:11:11 字数 234 浏览 0 评论 0原文

R 中有没有一种方法可以比较对象并返回有用的信息,例如差异在哪里?我需要比较文件,但愿意将它们读入 data.frames。这可能可以通过命令行更好地处理,但我想将我的测试封装到一个 R 脚本中。我的下一次尝试是使用 ddply 将每一行发送到 Compare() 函数并返回“FALSE”行的行号,但这仅在您进行一次插入或删除之前有效,然后其他所有内容都变为“FALSE”。

谢谢。

编辑:文件包含数字和字符数据的组合。

Is there a way in R to compare objects and return something useful like where the differences are? I need to compare files, but am willing to read them in to data.frames. This might just be handled better from the command line, but I would like to encapsulate my testing into one R script. My next attempt will be to use ddply to send each line to a compare() function and return the line numbers of the "FALSE" lines, but that only works until you have one insertion or deletion, then everything else becomes "FALSE".

Thanks.

EDIT: the files contain a combination of numeric and character data.

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

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

发布评论

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

评论(4

原野 2024-12-17 21:11:11

我知道已经有一段时间了,但是如果其他人偶然发现了这一点...

如果您只想查看差异,而不是在代码中使用它们,请参阅包 diffr。

install.packages("diffr")
library(diffr)
diffr("file1.txt", "file2.txt", contextSize = 0, minJumpSize = 500)

它并排显示两个文件的全部内容,并在 RStudio 查看器中突出显示差异。

I know it has been a while, but if someone else stumbles on this...

If you are only looking to view the differences, and not use them in code, see the package diffr.

install.packages("diffr")
library(diffr)
diffr("file1.txt", "file2.txt", contextSize = 0, minJumpSize = 500)

It shows the total contents of both files side by side with differences highlighted in the RStudio Viewer.

轮廓§ 2024-12-17 21:11:11

system(paste("fc",,, "> Difference.txt"))

似乎有效。我的 Google-fu 今天关闭了。

system(paste("fc", <file1>, <file2>, "> difference.txt"))

seems to work. My Google-fu was off today.

撩起发的微风 2024-12-17 21:11:11

由于听起来您的文件是纯文本,因此命令行 diff 工具非常适合此操作。 mac 和 unix 上都有一个内置的。语法很简单:

$ diff <file1> <file2>

还有许多其他可用的,以及针对不同操作系统的 GUI 包装器。在 Mac 上,我喜欢 KaleidscopeDelta Walker 如果您需要合并功能。在 Windows 上,GUI 标准是超越比较

Since it sounds like your files are plain text, a command line diff tool will work great for this. There is one built-in on mac and unix. Syntax is simply:

$ diff <file1> <file2>

There are a bunch of others available also, as well as GUI wrappers for different OSes. On Mac, I like Kaleidoscope and Delta Walker if you need merge capabilites. On Windows, the GUI standard is Beyond Compare.

情仇皆在手 2024-12-17 21:11:11

我已经能够在 Rmarkdown 中使用以下代码插入 diffr 函数的输出:

---
title: "Untitled"
output: html_document
---

## Including Plots

You can also embed plots, for example:
{r pressure, echo=FALSE}
library(diffr)

file1 <- tempfile()
writeLines("hello, world!\n", con = file1)
file2 <- tempfile()
writeLines(paste0("hello world?\nI don't get it\n", paste0(sample(letters, 65, replace = TRUE), collapse = "")), con = file2)

diffr(file1, file2, before = "f1", after = "f2")

之后,我们可以可视化 html 文件。

I have been able to insert the output of the diffr function with the following code in a Rmarkdown :

---
title: "Untitled"
output: html_document
---

## Including Plots

You can also embed plots, for example:
{r pressure, echo=FALSE}
library(diffr)

file1 <- tempfile()
writeLines("hello, world!\n", con = file1)
file2 <- tempfile()
writeLines(paste0("hello world?\nI don't get it\n", paste0(sample(letters, 65, replace = TRUE), collapse = "")), con = file2)

diffr(file1, file2, before = "f1", after = "f2")

Afterwards, we can visualize the html file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文