[R] matrix matching NA
Liaw, Andy
andy_liaw at merck.com
Wed Mar 1 15:25:51 CET 2006
Is this what you are looking for?
> m <- matrix(1:10, 5, 2)
> m[3, 1] <- m[5, 1] <- m[2, 2] <- NA
> m
[,1] [,2]
[1,] 1 6
[2,] 2 NA
[3,] NA 8
[4,] 4 9
[5,] NA 10
> m[complete.cases(m),]
[,1] [,2]
[1,] 1 6
[2,] 4 9
Andy
From: Fabian Lienert
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list