[R] comparing two data.frames

Marc Schwartz marc_schwartz at me.com
Fri Oct 30 01:20:44 CET 2009


On Oct 29, 2009, at 7:05 PM, Adrian Johnson wrote:

> Dear group,
> I have two data.frames X and Y identical except for values.
>
>> X
>    gene   fc
> A1CF   -0.10050677
> A2BP1  -2.03093217
> A2M      -0.09092704
> A4GALT 0.04124563
> A4GNT  -0.10336042
>
>> Y
>       gene   fc
> A1CF      -0.085709770
> A2BP1      1.058642812
> A2M         0.142530426
> A4GALT    0.009463123
> A4GNT     -0.057584059
>
> I am interested in finding A2BP1 that is negative in X and positive in
> Y. How could I search for reverse values.
> thank you.
> -Ad

Use ?merge and ?subset:

 > merge(X, Y, by = "gene")
     gene        fc.x         fc.y
1   A1CF -0.10050677 -0.085709770
2  A2BP1 -2.03093217  1.058642812
3    A2M -0.09092704  0.142530426
4 A4GALT  0.04124563  0.009463123
5  A4GNT -0.10336042 -0.057584059


 > subset(merge(X, Y, by = "gene"), (fc.x < 0) & (fc.y > 0))
    gene        fc.x      fc.y
2 A2BP1 -2.03093217 1.0586428
3   A2M -0.09092704 0.1425304


 > subset(merge(X, Y, by = "gene"),
          (fc.x < 0) & (fc.y > 0) & (gene == "A2BP1"))
    gene      fc.x     fc.y
2 A2BP1 -2.030932 1.058643


Alter the comparisons depending upon how you might need to deal with 0  
values.

HTH,

Marc Schwartz




More information about the R-help mailing list