显示文件或对象之间的差异
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道已经有一段时间了,但是如果其他人偶然发现了这一点...
如果您只想查看差异,而不是在代码中使用它们,请参阅包 diffr。
它并排显示两个文件的全部内容,并在 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.
It shows the total contents of both files side by side with differences highlighted in the RStudio Viewer.
system(paste("fc",,, "> Difference.txt"))
似乎有效。我的 Google-fu 今天关闭了。
system(paste("fc", <file1>, <file2>, "> difference.txt"))
seems to work. My Google-fu was off today.
由于听起来您的文件是纯文本,因此命令行
diff
工具非常适合此操作。 mac 和 unix 上都有一个内置的。语法很简单:还有许多其他可用的,以及针对不同操作系统的 GUI 包装器。在 Mac 上,我喜欢 Kaleidscope 和 Delta 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: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.
我已经能够在 Rmarkdown 中使用以下代码插入 diffr 函数的输出:
之后,我们可以可视化 html 文件。
I have been able to insert the output of the diffr function with the following code in a Rmarkdown :
Afterwards, we can visualize the html file.