Raymond Danner
2009-Dec-20 04:11 UTC
[R] Remove rows in a matrix that match rows in another matrix
Dear R Community, The following seems like a simple problem, but I''ve been stuck on it for some time, with no luck using matching or subsetting functions. I''m trying to remove the rows from a large matrix that match rows in another large matrix. A (small scale) example: col1<-c("A", "B", "C", "D") col2<-c("A", "B", "C", "D") m1<-cbind(col1, col2) col3<-c("B", "C", "D") col4<-c("B", "C", "z") m2<-cbind(col3, col4) Any ideas on how to get the following matrix? [,1] [,2] [1,] "A" "A" [2,] "D" "D" Thanks very much in advance, Ray [[alternative HTML version deleted]]
David Winsemius
2009-Dec-20 04:59 UTC
[R] Remove rows in a matrix that match rows in another matrix
On Dec 19, 2009, at 11:11 PM, Raymond Danner wrote:> Dear R Community, > > The following seems like a simple problem, but I've been stuck on it > for > some time, with no luck using matching or subsetting functions. I'm > trying > to remove the rows from a large matrix that match rows in another > large > matrix. A (small scale) example: > > col1<-c("A", "B", "C", "D") > col2<-c("A", "B", "C", "D") > m1<-cbind(col1, col2) > col3<-c("B", "C", "D") > col4<-c("B", "C", "z") > m2<-cbind(col3, col4) > > Any ideas on how to get the following matrix? > [,1] [,2] > [1,] "A" "A" > [2,] "D" "D" >I have an idea but I don't claim it to be the most elegant" apply(m1, 1, function(x) max( apply(m2, 1, function(y) all.equal(x, y, check.attributes=FALSE)) ) ) != "TRUE" [1] TRUE FALSE FALSE TRUE So....using logical indexing... m1[apply(m1, 1, function(x) max( # max(c(TRUE,FALSE))==TRUE apply(m2, 1, function(y) # now cycle through all of 2nd mtx all.equal(x, y, check.attributes=FALSE)) ) ) != "TRUE" , ] Last logical test "inverts" the result so you get the non-matched rows. col1 col2 [1,] "A" "A" [2,] "D" "D" all.equal is set up to allow ignoring the attributes (col names) whereas identical did not appear to allow that> Thanks very much in advance, > Ray > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
William Dunlap
2009-Dec-20 06:07 UTC
[R] Remove rows in a matrix that match rows in another matrix
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Raymond Danner > Sent: Saturday, December 19, 2009 8:12 PM > To: r-help at r-project.org > Subject: [R] Remove rows in a matrix that match rows in another matrix > > Dear R Community, > > The following seems like a simple problem, but I've been > stuck on it for > some time, with no luck using matching or subsetting > functions. I'm trying > to remove the rows from a large matrix that match rows in > another large > matrix. A (small scale) example: > > col1<-c("A", "B", "C", "D") > col2<-c("A", "B", "C", "D") > m1<-cbind(col1, col2) > col3<-c("B", "C", "D") > col4<-c("B", "C", "z") > m2<-cbind(col3, col4) > > Any ideas on how to get the following matrix? > [,1] [,2] > [1,] "A" "A" > [2,] "D" "D"One way is to paste together the columns to make a 'key' for the row and use match() or is.element() on the key. E.g., > key <- function(m)paste(sep="\1",m[,1],m[,2]) > m1[!is.element(key(m1),key(m2)),] col1 col2 [1,] "A" "A" [2,] "D" "D" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Thanks very much in advance, > Ray > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >