[R] omitting columns from a data frame

Ista Zahn izahn at psych.rochester.edu
Tue Jun 21 06:05:36 CEST 2011


Hi Erin,
As you've seen from the responses so far there are many ways to do it.
I like to do

xm1[setdiff(names(xm1), c("x1", "x2", ...))]

where xm1 is the data.frame, and "x1", "x2", ... are the columns you
wish to exclude. Just another option.

Best,
Ista
On Mon, Jun 20, 2011 at 11:52 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> Hi Erin,
>
> See inline.
>
> On Mon, Jun 20, 2011 at 8:45 PM, Erin Hodgess <erinm.hodgess at 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
>
>
>
>>
>> Thanks so much in advance!
>>
>> Sincerely,
>> Erin
>>
>>
>> --
>> Erin Hodgess
>> Associate Professor
>> Department of Computer and Mathematical Sciences
>> University of Houston - Downtown
>> mailto: erinm.hodgess at gmail.com
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>
> ______________________________________________
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list