[R] Extrat rows from a dataframe based on second data frame

Dieter Menne dieter.menne at menne-biomed.de
Mon Jul 25 20:14:05 CEST 2011


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


--
View this message in context: http://r.789695.n4.nabble.com/Extrat-rows-from-a-dataframe-based-on-second-data-frame-tp3693195p3693534.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list