Message-ID: <CAP01uR=kTn81m3LnZwg_1NXSFRTFgwXMNyDY7U7AgoB41=b2Ag@mail.gmail.com>
Date: 2012-05-20T14:52:27Z
From: Gabor Grothendieck
Subject: removeing only rows/columns with "na" value from square ( symmetrical ) matrix.
In-Reply-To: <CAGUDtZJOW7x3sjZsnqf7uAQN6gKFg+EMqq=-HBqKgcwgGaJu6g@mail.gmail.com>
On Sun, May 20, 2012 at 10:17 AM, Nevil Amos <nevil.amos at monash.edu> wrote:
> I have some square matrices with na values in corresponding rows and
> columns.
>
> M<-matrix(1:2,10,10)
> M[6,1:2]<-NA
> M[10,9]<-NA
> M<-as.matrix(as.dist(M))
> print (M)
>
> ? ?1 2 3 4 5 6 7 8 9 10
> 1 ? 0 ?2 1 2 1 NA 1 2 ?1 ?2
> 2 ? 2 ?0 1 2 1 NA 1 2 ?1 ?2
> 3 ? 1 ?1 0 2 1 ?2 1 2 ?1 ?2
> 4 ? 2 ?2 2 0 1 ?2 1 2 ?1 ?2
> 5 ? 1 ?1 1 1 0 ?2 1 2 ?1 ?2
> 6 ?NA NA 2 2 2 ?0 1 2 ?1 ?2
> 7 ? 1 ?1 1 1 1 ?1 0 2 ?1 ?2
> 8 ? 2 ?2 2 2 2 ?2 2 0 ?1 ?2
> 9 ? 1 ?1 1 1 1 ?1 1 1 ?0 NA
> 10 ?2 ?2 2 2 2 ?2 2 2 NA ?0
>
>
> How do I remove just the row/column pair( in this trivial example row 6 and
> 10 and column 6 and 10) containing the NA values?
>
> so that I end up with all rows/ columns that are not NA - e.g.
>
> ?1 2 3 4 5 7 8 9
> 1 0 2 1 2 1 1 2 1
> 2 2 0 1 2 1 1 2 1
> 3 1 1 0 2 1 1 2 1
> 4 2 2 2 0 1 1 2 1
> 5 1 1 1 1 0 1 2 1
> 7 1 1 1 1 1 0 2 1
> 8 2 2 2 2 2 2 0 1
> 9 1 1 1 1 1 1 1 0
>
Try this:
ix <- na.action(na.omit(replace(M, upper.tri(M), 0)))
M[-ix, -ix]
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com