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') > xa b [1,] 1 3 [2,] 2 4> x[,-'a']Error in -"a" : invalid argument to unary operator> x[,-1][1] 3 4
Jorge Ivan Velez
2009-Nov-03 01:35 UTC
[R] How to exclude certain columns by column names?
Hi Peng, Here is a suggestion: subset(x, select = -a) See ?subset for more details. HTH, Jorge On Mon, Nov 2, 2009 at 8:31 PM, Peng Yu <> wrote:> 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 > > ______________________________________________ > 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]]
Try this:> x[, colnames(x) != 'a'][1] 3 4 On Tue, Nov 3, 2009 at 9:31 AM, Peng Yu <pengyu.ut at gmail.com> wrote:> 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 > > ______________________________________________ > R-help at 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. >
Henrique Dallazuanna
2009-Nov-03 11:14 UTC
[R] How to exclude certain columns by column names?
Try this; x[,setdiff(colnames(x), 'a')] On Mon, Nov 2, 2009 at 11:31 PM, Peng Yu <pengyu.ut at gmail.com> wrote:> 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 > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O