On Wed, May 27, 2009 at 6:37 AM, Zeljko Vrba <zv...@ifi.uio.no> wrote:

> Given an arbitrary data frame, it is easy to exclude a column given its
> index:
> df[,-2].  How to do the same thing given the column name?  A naive attempt
> df[,-"name"] did not work :)
>

Various ways:

Boolean index vector:

    df[ , names(df) != "name" ]

List of wanted column names:

    df[ , setdiff(names(df), "name") ]

Negated list of unwanted column indexes:

   df[ , -match("name",names(df)) ]
   df[ , -which(names(df) == "name") ]

The special 'subset' hack for column names; beware, I think this is the only
place in R where you can negate a column name.

   subset(df , select = -a )

Hope this helps,

         -s

        [[alternative HTML version deleted]]

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

Reply via email to