ingo
ingo

Reputation: 37

subset matching columns but keep rows

I'm looking for a solution for following problem: I have 2 dataframes with multiple columns. When certain columns of the dataframes match I want to subset the rows as well. In this case columns of V1, V2 and V8 should match. The output (intersection) should contain also the rows. Appreciate any help.

Example

df1

row.names   V1  V2  V3  V4  V5  V6  V7  V8
1799    4   100333846   401070          C   C   1   1   hom
1936    21  38934599    exm-rs4372192   A   A   1   1   hom
1937    2   12790328    exm-rs1000053   G   G   1   1   hom

df2

row.names   V1  V2  V3  V4  V5  V6  V7  V8
1   1   762273      rs3115849   G   A   0   1   het
2   21  38934599    rs4372192   A   G   1   1   hom
3   1   877715      rs6605066   C   G   1   1   hom

Upvotes: 0

Views: 130

Answers (1)

Marcus-Rosti
Marcus-Rosti

Reputation: 16

I think your best bet is, https://stat.ethz.ch/R-manual/R-devel/library/base/html/merge.html

merge(df1,df2,by=c("V1","V2","V8"))

Upvotes: 0

Related Questions