### Re: [R] remove NA rows and columns

testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA tm1-testmatrix[,-which(apply(testmatrix,2,function(x)all(is.na(x] tm1 [,1] [,2] [1,] NA NA [2,]11 [3,]22 [4,]11 [5,]22 [6,] NA NA tm2-tm1[-which(apply(testmatrix,1,function(x)all(is.na(x,] tm2 [,1] [,2] [1,]11 [2,]22 [3,]11 [4,]22 Antje wrote: I guess, it's a rather simple thing but I cannot find a short way to reduce a matrix, removing all rows and columns having just NA elements. testmatrix - matrix(nrow=6, ncol=4) testmatrix[2:5,2:3] - seq(2) testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA the new matrix should look like this (by the way, I don't know which rows and columns are the one to be deleted... testmatrix [,1] [,2] [1,] 11 [2,] 22 [3,] 11 [4,] 22 -- View this message in context: http://www.nabble.com/remove-NA-rows-and-columns-tf4192605.html#a11923002 Sent from the R help mailing list archive at Nabble.com. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.

### Re: [R] remove NA rows and columns

Or, these operations can be called in one command: testmatrix[-which(apply(testmatrix,1,function(x)all(is.na(x,-which(apply(testmatrix,2,function(x)all(is.na(x] [,1] [,2] [1,]11 [2,]22 [3,]11 [4,]22 Vladimir Eremeev wrote: testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA tm1-testmatrix[,-which(apply(testmatrix,2,function(x)all(is.na(x] tm1 [,1] [,2] [1,] NA NA [2,]11 [3,]22 [4,]11 [5,]22 [6,] NA NA tm2-tm1[-which(apply(testmatrix,1,function(x)all(is.na(x,] tm2 [,1] [,2] [1,]11 [2,]22 [3,]11 [4,]22 Antje wrote: I guess, it's a rather simple thing but I cannot find a short way to reduce a matrix, removing all rows and columns having just NA elements. testmatrix - matrix(nrow=6, ncol=4) testmatrix[2:5,2:3] - seq(2) testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA the new matrix should look like this (by the way, I don't know which rows and columns are the one to be deleted... testmatrix [,1] [,2] [1,] 11 [2,] 22 [3,] 11 [4,] 22 -- View this message in context: http://www.nabble.com/remove-NA-rows-and-columns-tf4192605.html#a11923066 Sent from the R help mailing list archive at Nabble.com. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.

### Re: [R] remove NA rows and columns

Antje wrote: Hello, I guess, it's a rather simple thing but I cannot find a short way to reduce a matrix, removing all rows and columns having just NA elements. testmatrix - matrix(nrow=6, ncol=4) testmatrix[2:5,2:3] - seq(2) testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA the new matrix should look like this (by the way, I don't know which rows and columns are the one to be deleted... testmatrix [,1] [,2] [1,] 11 [2,] 22 [3,] 11 [4,] 22 testmatrix[!apply(is.na(testmatrix), 1, all), !apply(is.na(testmatrix), 2, all)] [,1] [,2] [1,]11 [2,]22 [3,]11 [4,]22 Ciao, Antje __ R-help@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 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 __ R-help@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 and provide commented, minimal, self-contained, reproducible code.

### Re: [R] remove NA rows and columns

Hello, thank you both very much! It is as easy as expected... (I think I still have to learn a lot!) Have a nice day! Antje Vladimir Eremeev schrieb: Or, these operations can be called in one command: testmatrix[-which(apply(testmatrix,1,function(x)all(is.na(x,-which(apply(testmatrix,2,function(x)all(is.na(x] [,1] [,2] [1,]11 [2,]22 [3,]11 [4,]22 Vladimir Eremeev wrote: testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA tm1-testmatrix[,-which(apply(testmatrix,2,function(x)all(is.na(x] tm1 [,1] [,2] [1,] NA NA [2,]11 [3,]22 [4,]11 [5,]22 [6,] NA NA tm2-tm1[-which(apply(testmatrix,1,function(x)all(is.na(x,] tm2 [,1] [,2] [1,]11 [2,]22 [3,]11 [4,]22 Antje wrote: I guess, it's a rather simple thing but I cannot find a short way to reduce a matrix, removing all rows and columns having just NA elements. testmatrix - matrix(nrow=6, ncol=4) testmatrix[2:5,2:3] - seq(2) testmatrix [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA11 NA [3,] NA22 NA [4,] NA11 NA [5,] NA22 NA [6,] NA NA NA NA the new matrix should look like this (by the way, I don't know which rows and columns are the one to be deleted... testmatrix [,1] [,2] [1,] 11 [2,] 22 [3,] 11 [4,] 22 __ R-help@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 and provide commented, minimal, self-contained, reproducible code.