# [R] matrix matching NA

Fabian Lienert flienert at student.ethz.ch
Wed Mar 1 14:44:45 CET 2006

```Dear R list members,

I am seeking for an elegant way for matching NA elements of a matrix.
Everthing I tried, the result was a vector.

Here ist an example with vectors that works:
a and b are vectors of same lenght representing pairs of data (a[i]
b[i]) with some NA elements:

## 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
## /R Code

To get two vectors without any NA elements I do the following:

## R Code
awithoutan <- a[(1:length(a))[(!is.na(a))&(!is.na(b))]]
bwithoutan <- b[(1:length(b))[(!is.na(a))&(!is.na(b))]]

awithoutan
1 4 5
bwithoutan
6 9 10
## /R Code

How can I do the same matching in a matrix and get a matrix without NA
elements (and less colums of course)?

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

Gives me always a vector not a matrix.

Thanks a lot for any hint,
Fabian
--
Fabian Lienert, Climatology Student ETH Zurich, Switzerland

```