[R] Extract NULL column in a matrix e.g. matrix[,-NULL]

Vincent Goulet vincent.goulet at act.ulaval.ca
Sun Feb 11 18:30:02 CET 2007


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 Lapointe

Would

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



More information about the R-help mailing list