[R] How to exclude certain columns by column names?

Peng Yu pengyu.ut at gmail.com
Tue Nov 3 02:31:25 CET 2009


I can exclude columns by column number using '-'. But I wondering if
there is an easy way to exclude some columns by column names.

> x=cbind(c(1,2),c(3,4))
> x
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> colnames(x)=c('a','b')
> x
     a b
[1,] 1 3
[2,] 2 4
> x[,-'a']
Error in -"a" : invalid argument to unary operator
> x[,-1]
[1] 3 4




More information about the R-help mailing list