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