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.
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?
Henrique Dallazuanna
2009-Jul-06 17:25 UTC
[R] Extracting only the non-repeated rows of matrices
Try this:
subset(merge(as.data.frame(A),
cbind(as.data.frame(B), ID=1), all = T), is.na(ID), select = -ID)
On Mon, Jul 6, 2009 at 8:39 AM, dreamworx <Andy_woolston@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@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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]
Apparently Analagous Threads
- matching rows in matrices
- extract same columns and rows in two matrices
- Deleting repeated rows
- extracting one element of correlation matrices from a list poroduced by the 'by' statement
- changing the signs in rows or columns in matrices and check them if they are identical