[R] Differing rows in two data.frames?

Jesse Brown jesse.r.brown at lmco.com
Tue Aug 18 13:32:43 CEST 2009


Mark Knecht wrote:
> On Mon, Aug 17, 2009 at 2:25 PM, Mark Knecht<markknecht at gmail.com> wrote:
>   
>> Hi,
>>   What commands would I look at to compare row-by-row two data frames
>> of the same size and print out any differences between the two?
>>
>> Thanks,
>> Mark
>>
>>     
>
> So I got an answer using the following code:
>
> XXX = cbind(SystemWithCP, SystemWithoutCP)
> head(XXX)
> names(XXX)[14] = "NewPLPos"
> head(XXX)
> YYY = subset(XXX, XXX$PL_Pos != XXX$NewPLPos)
>
> write.csv(YYY, "C:\\Dave1.csv")
>
> This works (I suppose) because the two data.frames are exactly the
> same size. Seems like there probably a much better way to do it but
> this seems to work.
>
> - Mark
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   
Sorry if this ends up being a dup, I forgot to include the list in the reply.

yyy <- SystemWithCP - SystemWithoutCP
yyy[apply(yyy,1,function(x) any(x != 0))]


This gives you the row they differ on and the column that is different, but not the actual values.
You might also try a modification on what you have above

yyy <- subset(cbind(SystemWithCP,SystemWithoutCP), SystemWithCP$PL_Pos != SystemWithoutCP$PL_Pos)


Which gives you the same as your example without the modified names.
I'm not entirely clear on if you need to retain the values or just that they are different.


- Jesse




More information about the R-help mailing list