[R] matrix matching NA

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Mar 1 15:19:09 CET 2006


probably you'll find ?complete.cases() useful

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Fabian Lienert" <flienert at student.ethz.ch>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, March 01, 2006 2:44 PM
Subject: [R] matrix matching NA


> 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
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list