Show difference between files or objects

为君一笑 提交于 2021-01-27 06:09:12

问题


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.


回答1:


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.




回答2:


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

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




回答3:


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.



来源:https://stackoverflow.com/questions/7811878/show-difference-between-files-or-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!