Dear list, I would like to extract from a matrix all those rows, that are unique. By unique, I don't mean the unique that is accomplished by the function unique(), though... Consider the following example: > h [,1] [,2] [1,] 4 4 [2,] 1 4 [3,] 4 1 Now unique(h) returns exactly the same - because 1 4 and 4 1 is not the same for that function. What I would like to see, though, are only the first two rows (or the first and the third, it does not matter). Does anybody know how to do that? Cheers, Dax.
Dear Dax, I'll bet that someone comes up with a better approach, but the following does appear to work: u <- unique(t(sapply(as.data.frame(t(h)), sort))) I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of dax42 > Sent: Saturday, January 29, 2005 7:54 AM > To: r-help at stat.math.ethz.ch > Subject: [R] unique rows > > Dear list, > > I would like to extract from a matrix all those rows, that are unique. > By unique, I don't mean the unique that is accomplished by > the function unique(), though... > > Consider the following example: > > h > [,1] [,2] > [1,] 4 4 > [2,] 1 4 > [3,] 4 1 > > Now unique(h) returns exactly the same - because 1 4 and 4 1 > is not the same for that function. > What I would like to see, though, are only the first two rows > (or the first and the third, it does not matter). > > Does anybody know how to do that? > Cheers, Dax. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
There may be more efficient ways, but unique(t(apply(h, 1, sort))) does what I think you want. Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") dax42 wrote:> Dear list, > > I would like to extract from a matrix all those rows, that are unique. > By unique, I don't mean the unique that is accomplished by the > function unique(), though... > > Consider the following example: > > h > [,1] [,2] > [1,] 4 4 > [2,] 1 4 > [3,] 4 1 > > Now unique(h) returns exactly the same - because 1 4 and 4 1 is not > the same for that function. > What I would like to see, though, are only the first two rows (or the > first and the third, it does not matter). > > Does anybody know how to do that? > Cheers, Dax. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
On 29-Jan-05 dax42 wrote:> Dear list, > > I would like to extract from a matrix all those rows, that are unique. > By unique, I don't mean the unique that is accomplished by the function > unique(), though... > > Consider the following example: > > h > [,1] [,2] > [1,] 4 4 > [2,] 1 4 > [3,] 4 1 > > Now unique(h) returns exactly the same - because 1 4 and 4 1 is not the > same for that function. > What I would like to see, though, are only the first two rows (or the > first and the third, it does not matter). > > Does anybody know how to do that? > Cheers, Dax.How about: h[!duplicated(t(apply(h,1,sort))),] [,1] [,2] [1,] 4 4 [2,] 1 4 Better than unique(t(apply(h,1,sort))) [,1] [,2] [1,] 4 4 [2,] 1 4 in general (though it comes to the same for your example) since it preserves the order of elements in each row. Cheers, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 29-Jan-05 Time: 14:26:31 ------------------------------ XFMail ------------------------------