[R] excluding a column from a data frame

Peter Alspach PAlspach at hortresearch.co.nz
Wed Apr 15 07:51:35 CEST 2009


Tena koe Erin

xx[, names(xx)!='x2']

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Erin Hodgess
> Sent: Wednesday, 15 April 2009 5:39 p.m.
> To: R help
> Subject: [R] excluding a column from a data frame
> 
> Dear R People:
> 
> Suppose I have the following data frame:
> 
>           x1         x2       x3
> 1 -0.1582116 0.06635783 1.765448
> 2 -1.1407422 0.47235664 0.615931
> 3  0.8702362 2.32301341 2.653805
> > str(xx)
> 'data.frame':   3 obs. of  3 variables:
>  $ x1: num  -0.158 -1.141 0.87
>  $ x2: num  0.0664 0.4724 2.323
>  $ x3: num  1.765 0.616 2.654
> 
> I can exclude the second column nicely via:
> > xx[,-2]
>           x1       x3
> 1 -0.1582116 1.765448
> 2 -1.1407422 0.615931
> 3  0.8702362 2.653805
> 
> Now suppose I wanted to exclude the column called "x2".  If I try:
> > xx[,-"x2"]
> Error in -"x2" : invalid argument to unary operator
> >
> 
> things don't work.  Is there a simple way to do this by name 
> rather than number, please?
> 
> Thanks,
> 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.
> 

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.




More information about the R-help mailing list