Lida Zeighami
2015-Sep-09 19:39 UTC
[R] intersection between two matrices based on two columns in R
Hi there, I want to find the intersection between two different data frame or matrices based on two columns. for example in matrix A I have 5 columns, the first two columns are Id1 and Id2 and I have the same columns in the other matrix B, (Id1, Id2 ,,,) how can I find the intersection between these two matrices based on columns Id1 and Id2 in R? Thanks [[alternative HTML version deleted]]
Jeff Newmiller
2015-Sep-09 19:48 UTC
[R] intersection between two matrices based on two columns in R
Matrices and data frames are write different. For this you most likely want to use days frames. ?merge Using typical options merge gives you all columns from both data frames. You can choose to select specific columns by indexing the data frames before passing them to merge if you don't want that. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On September 9, 2015 12:39:43 PM PDT, Lida Zeighami <lid.zigh at gmail.com> wrote:>Hi there, > >I want to find the intersection between two different data frame or >matrices based on two columns. >for example in matrix A I have 5 columns, the first two columns are Id1 >and >Id2 and I have the same columns in the other matrix B, (Id1, Id2 ,,,) >how can I find the intersection between these two matrices based on >columns >Id1 and Id2 in R? > >Thanks > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
Bert Gunter
2015-Sep-09 19:51 UTC
[R] intersection between two matrices based on two columns in R
I am not quite clear what you mean by "intersection", but I think ?merge is what you are looking for. Cheers, Bert P.S. Please post in plain text, not HTML (though here it didn't matter). Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Wed, Sep 9, 2015 at 12:39 PM, Lida Zeighami <lid.zigh at gmail.com> wrote:> Hi there, > > I want to find the intersection between two different data frame or > matrices based on two columns. > for example in matrix A I have 5 columns, the first two columns are Id1 and > Id2 and I have the same columns in the other matrix B, (Id1, Id2 ,,,) > how can I find the intersection between these two matrices based on columns > Id1 and Id2 in R? > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Duncan Murdoch
2015-Sep-09 19:51 UTC
[R] intersection between two matrices based on two columns in R
On 09/09/2015 3:48 PM, Jeff Newmiller wrote:> Matrices and data frames are write different. For this you most likely want to use days frames....> Sent from my phone. Please excuse my brevity.... and your auto-correct. ;-) Duncan Murdoch
David Winsemius
2015-Sep-09 19:53 UTC
[R] intersection between two matrices based on two columns in R
On Sep 9, 2015, at 12:39 PM, Lida Zeighami wrote:> Hi there, > > I want to find the intersection between two different data frame or > matrices based on two columns. > for example in matrix A I have 5 columns, the first two columns are Id1 and > Id2 and I have the same columns in the other matrix B, (Id1, Id2 ,,,) > how can I find the intersection between these two matrices based on columns > Id1 and Id2 in R? >It's not clear to me what you mean by the intersection of matrices, but if you want the intersection of a column vector, A[ ,'Id1'] , with another column vector, B[ , 'Id1'], then this produces the intersection (as a vector): intersect( A[ ,'Id1'] , B[ , 'Id1']) I suspect, however that your native language is not R or "database" (and perhaps not English) so my alternate hypothesis is that you really intend to _merge_ these "matrices", which I suspect are really dataframes: my_inner_join <- merge(A, B, by=c("Id1", "Id2") )> Thanks > > [[alternative HTML version deleted]] >This is a plain text mailing list. -- David Winsemius Alameda, CA, USA