[R] Removing columns that are all NA from a matrix

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Feb 14 14:11:29 CET 2008


try this:

mat <- matrix(rnorm(42), 6, 7)
mat[sample(42, 10)] <- NA
mat[, c(3,5)] <- NA

ind <- colSums(is.na(mat)) != nrow(mat)
mat
mat[, ind]


I hope it helps.

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://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Martin Waller" <martinej.waller at ntlworld.com>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, February 14, 2008 1:59 PM
Subject: [R] Removing columns that are all NA from a matrix


> Hi,
>
> I guess this might be a FAQ or something, and there's probably a 
> nice
> simple way to do it, but I can't think of it:
>
> Given a matrix, I want to remove columns that are _entirely_ filled 
> with
> NAs (partial NAs are fine).
>
> How please?
>
> Thanks,
>
> Martin
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


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



More information about the R-help mailing list