[R] excluding a column from a data frame

Bert Gunter gunter.berton at gene.com
Wed Apr 15 17:33:19 CEST 2009


How about: 

xx[,-match("x2",names(xx))]

or

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

etc. 


Bert Gunter
Genentech Nonclinical Biostatistics
650-467-7374

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Erin Hodgess
Sent: Tuesday, April 14, 2009 10:39 PM
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.




More information about the R-help mailing list