Extrat rows from a dataframe based on second data frame

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Extrat rows from a dataframe based on second data frame

Bansal, Vikas
Dear all,

I have also attached my question in txt file because of bad column spacing.

I have two data frames

df

Chr            Pos         CaseA     CaseC        CaseG     CaseT
  10 135349878  0.000000  3.428571  0.000000 20.571429
  10 135349880  0.000000 21.333333  0.000000  2.666667
  10 135349883 21.000000  0.000000  3.000000  0.000000
  10 135349912  0.000000  0.000000 21.000000  3.000000
  10 135349913  0.000000 21.000000  0.000000  3.000000


mydf

V1        V2       V3 V4 V5 V6 V7 V8    V9        V10
 10 135349459  a  A 14  0  0  7   ,.,,,.,    bSa`^aa
 10 135349878  a  A 11  0  0  7   ,,,,,..    aaab^ab
 10 135349877  g  G 18  0  0  7   ,,,,,..   `a`_\\`b
 10 135349912  g  G 18  0  0  8  ,,,,,..,   `_abXbba
 10 135349881  a  A 14  0  0  8  ,,,,..,.   a`_aab_a

I want to have a new data frame with all rows in mydf whose 2nd column matches the second column of df.
so output of new data frame-

newdf

V1       V2       V3 V4 V5 V6 V7 V8    V9        V10
 10 135349878  a  A 11  0  0  7   ,,,,,..    aaab^ab
 10 135349912  g  G 18  0  0  8  ,,,,,..,   `_abXbba

Can you please help me.I will be very thankful to you.



Thanking you,
Warm Regards
Vikas Bansal
Msc Bioinformatics
Kings College London
______________________________________________
[hidden email] 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.

problem.txt (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Extrat rows from a dataframe based on second data frame

Dieter Menne
Bansal, Vikas wrote
I have two data frames

df

Chr            Pos         CaseA     CaseC        CaseG     CaseT
  10 135349878  0.000000  3.428571  0.000000 20.571429
  10 135349880  0.000000 21.333333  0.000000  2.666667
  10 135349883 21.000000  0.000000  3.000000  0.000000
  10 135349912  0.000000  0.000000 21.000000  3.000000
  10 135349913  0.000000 21.000000  0.000000  3.000000


mydf

V1        V2       V3 V4 V5 V6 V7 V8    V9        V10
 10 135349459  a  A 14  0  0  7   ,.,,,.,    bSa`^aa
 10 135349878  a  A 11  0  0  7   ,,,,,..    aaab^ab
 10 135349877  g  G 18  0  0  7   ,,,,,..   `a`_\\`b
 10 135349912  g  G 18  0  0  8  ,,,,,..,   `_abXbba
 10 135349881  a  A 14  0  0  8  ,,,,..,.   a`_aab_a

I want to have a new data frame with all rows in mydf whose 2nd column matches the second column of df.
..
(not tested)
merge(mydf,df[,"Pos",drop=FALSE], by.x=V2,by.y="Pos")

Not sure if drop=FALSE is needed.

Dieter