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
data <- data[ , !apply(is.na(data), 2, all)] (or something like that) G. On Thu, Feb 14, 2008 at 12:59:46PM +0000, Martin Waller wrote:> 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
On 2/14/2008 7:59 AM, Martin Waller wrote:> 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?mymat[,which(colMeans(is.na(mymat)) < 1)]> 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Dimitris Rizopoulos
2008-Feb-14 13:11 UTC
[R] Removing columns that are all NA from a matrix
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
On 14-Feb-08 12:59:46, Martin Waller wrote:> 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, > MartinIt probably isn't a FAQ (it's a somewhat special question) but the following illustrates one way to do it: M<-cbind(c(1,2,3,4),c(1,2,NA,4),c(NA,NA,NA,NA)) M ## [,1] [,2] [,3] ##[1,] 1 1 NA ##[2,] 2 2 NA ##[3,] 3 NA NA ##[4,] 4 4 NA apply(M,2,function(x){!all(is.na(x))}) ##[1] TRUE TRUE FALSE M[,apply(M,2,function(x){!all(is.na(x))})] ## [,1] [,2] ##[1,] 1 1 ##[2,] 2 2 ##[3,] 3 NA ##[4,] 4 4 Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 14-Feb-08 Time: 13:14:20 ------------------------------ XFMail ------------------------------
Martin Waller
2008-Feb-14 13:35 UTC
[R] (RESOLVED) Re: Removing columns that are all NA from a matrix
Thanks to all who responded - great stuff! Martin Martin Waller wrote:> 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. >
Apparently Analagous Threads
- counts of each column that are not NA, and/or greater than column means
- Replacing all NA values in a matrix
- Replacing NAs in a data frame using is.na() fails if there are no NAs
- extract columns of a matrix/data frame
- Converting a list to a matrix - I still don't think I have it right