But isn't this version of which() typo-proof?
> x <- iris[-which(c("Sepal.Length", "SSSepal.Width") %in% names(iris))]
Btw, I prefer the following, ie. simply assigning to NULL. Much easier notation.
> y <- iris
> y$Sepal.Width <- y$SSSepal.Width <- NULL


 Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have the 
Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




________________________________
From: Joshua Wiley <jwiley.psych@gmail.com>
To: Ista Zahn <izahn@psych.rochester.edu>
Cc: R-Help <r-help@stat.math.ethz.ch>
Sent: Tue, June 21, 2011 5:05:27 PM
Subject: Re: [R] omitting columns from a data frame

On Tue, Jun 21, 2011 at 6:57 AM, Ista Zahn <izahn@psych.rochester.edu> wrote:
> I would cation people not to use the -which strategy because entering
> a value that doesn't exist as a column name returns a zero-column
> data.frame, without so much as a warning. This can be a problem if you
> don't know if a column exists but just want to make sure it doesn't,
> or if you make a typo. Compare

Good point.  In some ways, I am a little unsettled by setdiff()
because if you make a typo, you may *think* you have omitted it, and
you will have a sensible data frame, but it will actually still be
there.  I am particularly thinking of the case where you are omitting
several variables at once:

mtcars[setdiff(names(mtcars), c("disp", "jp"))]

which is why my current preference has been match().  The default for
no match fails spectacularly if the variable does not exist:

mtcars[-match(c("disp", "jp"), names(mtcars))]

of course, this would not work for your example of a variable you just
want to make sure is deleted.  Anyone have thoughts on pitfalls of
match?

Josh

> head(mtcars[, -which(names(mtcars) == "make.sure.to.delete")])
>
> to
>
> head(mtcars[, setdiff(names(mtcars), "make.sure.to.delete")])
>
> Best,
> Ista
>
> On Tue, Jun 21, 2011 at 12:22 AM, Joshua Wiley <jwiley.psych@gmail.com> wrote:
>> On Mon, Jun 20, 2011 at 8:55 PM, Erin Hodgess <erinm.hodgess@gmail.com> 
wrote:
>>> Too funny!
>>>
>>> how about subset?
>>
>> Sure, that is one option.  Each of the following will also work.  The
>> ones wrapped with c() can easily omit more than one at a time.
>>
>> mtcars[, -which(names(mtcars) == "drat")]
>> mtcars[, names(mtcars) != "drat"]
>> mtcars[, !names(mtcars) %in% c("drat")]
>> mtcars[, -match(c("drat"), names(mtcars))]
>>
>>>
>>> On Mon, Jun 20, 2011 at 10:52 PM, Joshua Wiley <jwiley.psych@gmail.com> 
>wrote:
>>>> Hi Erin,
>>>>
>>>> See inline.
>>>>
>>>> On Mon, Jun 20, 2011 at 8:45 PM, Erin Hodgess <erinm.hodgess@gmail.com> 
>>wrote:
>>>>> Dear R People:
>>>>>
>>>>> I have a data frame, xm1, which has 12 rows and 4 columns.
>>>>>
>>>>> If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is as
>>>>> it should be.
>>>>
>>>> Okay, so you know how to use the column number to omit columns.
>>>>
>>>>>
>>>>> Now, is there a way to use the names of the columns to omit them, please?
>>>>
>>>> You have all the pieces (the column names, and the knowledge that you
>>>> can omit columns by their index).
>>>>
>>>> Homework: find a way to return the column numbers given the column names 
>>>>(hint).
>>>>
>>>> Cheers,
>>>>
>>>> Josh
>>>>
>>>>
>>>>
>>>>>
[[elided Yahoo spam]]
>>>>>
>>>>> Sincerely,
>>>>> Erin
>>>>>
>>>>>
>>>>> --
>>>>> Erin Hodgess
>>>>> Associate Professor
>>>>> Department of Computer and Mathematical Sciences
>>>>> University of Houston - Downtown
>>>>> mailto: erinm.hodgess@gmail.com
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Joshua Wiley
>>>> Ph.D. Student, Health Psychology
>>>> University of California, Los Angeles
>>>> http://www.joshuawiley.com/
>>>>
>>>
>>>
>>>
>>> --
>>> Erin Hodgess
>>> Associate Professor
>>> Department of Computer and Mathematical Sciences
>>> University of Houston - Downtown
>>> mailto: erinm.hodgess@gmail.com
>>>
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> http://www.joshuawiley.com/
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org

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

