# [R] matrix matching NA

Fabian Lienert flienert at student.ethz.ch
Wed Mar 1 15:26:05 CET 2006

```Dear list members,

Berwin gave me the right hint, it works!
Look at this:
## R Code
a <- (1:5)
a[2] <- NA
b <- (6:10)
b[3] <- NA

a
1  NA  3  4  5
b
6  7  NA  9 10

matrix <- array(1:5*2, dim=c(5,2))
matrix[,1] <- a
matrix[,2] <- b

matrix[ !apply(is.na(matrix), 1, any),]

[,1] [,2]
[1,]    1    6
[2,]    4    9
[3,]    5   10

## /R Code

Cheers,

Fabian

Berwin A Turlach wrote:
> G'day Fabian,
>
>
>>>>>>"FL" == Fabian Lienert <flienert at student.ethz.ch> writes:
>
>
>     FL> ## R Code
>     FL> awithoutan <- a[(1:length(a))[(!is.na(a))&(!is.na(b))]]
> a[(!is.na(a))&(!is.na(b))] ## works too
>
>     FL> bwithoutan <- b[(1:length(b))[(!is.na(a))&(!is.na(b))]]
> b[(!is.na(a))&(!is.na(b))] ## ditto
>
>     FL> How can I do the same matching in a matrix and get a matrix without NA
>     FL> elements (and less colums of course)?
>
>     FL> matrix <- array(1:5*2, dim=c(5,2))
> Not a good name, since matrix is also a function, could lead to
> confusion in more complicated situations. :)
>
>     FL> matrix[,1] <- a
>     FL> matrix[,2] <- b
>     FL> matrix[,][(!is.na(matrix[,]))]
>
>>matrix[ !apply(is.na(matrix), 1, any), ]
>
>      [,1] [,2]
> [1,]    1    6
> [2,]    4    9
> [3,]    5   10
>
> Hope this helps.
>
> Cheers,
>
>         Berwin
>

--
Beste Grüsse,

Fabian Lienert

--
pgp public key: http://lienert.org/keys/lienert.asc

```