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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Feb 11 19:04:19 CET 2007


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.
>



More information about the R-help mailing list