[R] Extracting only the non-repeated rows of matrices

jim holtman jholtman at gmail.com
Mon Jul 6 19:17:12 CEST 2009


Here is one way of doing it:

> set.seed(1)
> a <- matrix(sample(1:2, 10, TRUE), ncol=2)
> b <- matrix(sample(1:2, 4, TRUE), ncol=2)
> a
     [,1] [,2]
[1,]    1    2
[2,]    1    2
[3,]    2    2
[4,]    2    2
[5,]    1    1
> b
     [,1] [,2]
[1,]    1    2
[2,]    1    1
> # create a vector of values
> a.s <- apply(a, 1, paste, collapse=';')
> b.s <- apply(b, 1, paste, collapse=';')
> a[!(a.s %in% b.s),]
     [,1] [,2]
[1,]    2    2
[2,]    2    2
>


On Mon, Jul 6, 2009 at 7:39 AM, dreamworx<Andy_woolston at hotmail.com> wrote:
>
> I have 2 matrices. One which contains a complete set of row combinations.
> Another which contains a subset of the first. What I would like to extract
> is the rows which are in the first but not repeated in the second. For
> instance if the following were my two matrices,
>
> A
>       [,1] [,2] [,3] [,4]
>  [1,]    1    2    3    4
>  [2,]    1    2    3    5
>  [3,]    1    2    3    6
>  [4,]    1    2    3    7
>  [5,]    1    2    3    8
>  [6,]    1    2    3    9
>
>
> B
>       [,1] [,2] [,3] [,4]
>  [1,]    1    2    3    4
>  [2,]    1    2    3    5
>  [3,]    1    2    3    6
>  [4,]    1    2    3    9
>
>
> I would like to extract the following,
>
>       [,1] [,2] [,3] [,4]
>  [1,]    1    2    3    7
>  [2,]    1    2    3    8
>
> I would like to know if there is a simple solution to this problem? If I
> added the matrices and used the unique function, could I track which rows
> have been deleted? Otherwise, I can only think of performing a loop with an
> if statement.
> --
> View this message in context: http://www.nabble.com/Extracting-only-the-non-repeated-rows-of-matrices-tp24353933p24353933.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list