Pierre Lapointe
2007-Feb-11 16:42 UTC
[R] Extract NULL column in a matrix e.g. matrix[,-NULL]
Hello, I need to remove columns in a matrix. The number of columns varies from 0 to n. I can't figure out how to specify the zero case. aa <-matrix(runif(5^2),5,5) #remove column 3 aa[,-3] #remove no column aa[,-NULL] Error in -NULL : invalid argument to unary operator I know I could use ifelse, but it would complicate my model a lot. Is there a direct way to specify that the number of columns to remove is 0? Pierre Lapointe [[alternative HTML version deleted]]
Vincent Goulet
2007-Feb-11 17:30 UTC
[R] Extract NULL column in a matrix e.g. matrix[,-NULL]
Le 07-02-11 ? 11:42, Pierre Lapointe a ?crit :> Hello, > > I need to remove columns in a matrix. The number of columns varies > from 0 > to n. I can't figure out how to specify the zero case. > > aa <-matrix(runif(5^2),5,5) > > #remove column 3 > > aa[,-3] > > #remove no column > > aa[,-NULL] > > Error in -NULL : invalid argument to unary operator > > I know I could use ifelse, but it would complicate my model a lot. > Is there > a direct way to specify that the number of columns to remove is 0? > > Pierre LapointeWould aa <- if (n > 0) aa[, -n] else aa still be too complicated? --- Vincent Goulet, Associate Professor ?cole d'actuariat Universit? Laval, Qu?bec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca
Gabor Grothendieck
2007-Feb-11 18:04 UTC
[R] Extract NULL column in a matrix e.g. matrix[,-NULL]
Try: aa[, !seq(ncol(aa)) %in% NULL] On 2/11/07, Pierre Lapointe <pierrelap at gmail.com> wrote:> Hello, > > > > I need to remove columns in a matrix. The number of columns varies from 0 > to n. I can't figure out how to specify the zero case. > > > > aa <-matrix(runif(5^2),5,5) > > > > #remove column 3 > > aa[,-3] > > > > #remove no column > > aa[,-NULL] > > Error in -NULL : invalid argument to unary operator > > > > I know I could use ifelse, but it would complicate my model a lot. Is there > a direct way to specify that the number of columns to remove is 0? > > > > Pierre Lapointe > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >