> -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Raymond Danner > Sent: Saturday, December 19, 2009 8:12 PM > To: r-help@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@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. > ______________________________________________ R-help@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.