Message-ID: <8d4c23b10911021736u421b835ahc4f1dc88bc248bae@mail.gmail.com>
Date: 2009-11-03T01:36:33Z
From: Linlin Yan
Subject: How to exclude certain columns by column names?
In-Reply-To: <366c6f340911021731y5bccdad1oa406d04e38fa797a@mail.gmail.com>
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.
>