[R] Remove rows in a matrix that match rows in another matrix

William Dunlap wdunlap at tibco.com
Sun Dec 20 07:07:39 CET 2009


> -----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.
> 




More information about the R-help mailing list