useR's, I have a matrix given by the code: mat <- matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10),10,5) This is a 10x5 matrix containing missing values. All columns except the second contain missing values. I want to delete all columns that contain ALL missing values, and in this case, it would be the first and fourth columns. Any column that has at least one real number would remain. I know I can use "mat[,-1]" to delete the first column, but I have a much larger matrix where it is impossible to tell how many columns contain all missing values and which don't. Is there a function or something else that may be able to help me accomplish this? Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/Deleting-columns-from-a-matrix-tp23695656p23695656.html Sent from the R help mailing list archive at Nabble.com.
one way is: mat <- matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10), 10, 5) ind <- colSums(is.na(mat)) != nrow(mat) mat[, ind] I hope it helps. Best, Dimitris dxc13 wrote:> useR's, > I have a matrix given by the code: > mat <- > matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10),10,5) > > This is a 10x5 matrix containing missing values. All columns except the > second contain missing values. I want to delete all columns that contain > ALL missing values, and in this case, it would be the first and fourth > columns. Any column that has at least one real number would remain. I know > I can use "mat[,-1]" to delete the first column, but I have a much larger > matrix where it is impossible to tell how many columns contain all missing > values and which don't. > Is there a function or something else that may be able to help me accomplish > this? > > Thanks in advance. > dxc13-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Dear dxc13, Here is another way: index <- apply(mat, 2, function(x) !all(is.na(x))) mat[ , index] HTH, Jorge On Sun, May 24, 2009 at 12:53 PM, dxc13 <dxc13@health.state.ny.us> wrote:> > useR's, > I have a matrix given by the code: > mat <- > > matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10),10,5) > > This is a 10x5 matrix containing missing values. All columns except the > second contain missing values. I want to delete all columns that contain > ALL missing values, and in this case, it would be the first and fourth > columns. Any column that has at least one real number would remain. I know > I can use "mat[,-1]" to delete the first column, but I have a much larger > matrix where it is impossible to tell how many columns contain all missing > values and which don't. > Is there a function or something else that may be able to help me > accomplish > this? > > Thanks in advance. > dxc13 > -- > View this message in context: > http://www.nabble.com/Deleting-columns-from-a-matrix-tp23695656p23695656.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]