0

Possible Duplicate:
Drop Columns R Data frame

I know that I can set a column to NULL (i.e. data$todrop = NULL ), but is there a more concise way to subset a data.frame or matrix to all columns but one? I use the following pattern:

data[ , !colnames( data ) %in% "todrop" ]

this doesn't seem to work:

data[ , -"todrop" ]

note that I do not know the index of column "todrop" a-priori

Community
  • 1
  • 1
SFun28
  • 32,209
  • 43
  • 123
  • 233
  • 2
    There are several ways, some of which are more concise than others; all of which can be found by searching. :) For example: [Drop Columns R Data frame](http://stackoverflow.com/questions/4605206/drop-columns-r-data-frame) – Joshua Ulrich Oct 06 '11 at 22:25
  • Thanks for the link...after review it seems that there is only one concise way to do this: subset( data, select = -c(todrop) ), which of course causes a NOTE (or WARNING?) when running R CMD check because todrop is not defined. – SFun28 Oct 06 '11 at 22:32
  • That question was only _one_ example. There's also [How do you remove columns from a data.frame?](http://stackoverflow.com/questions/7072159/how-do-you-remove-columns-from-a-data-frame) and [How to drop columns by name in a data frame](http://stackoverflow.com/questions/5234117/how-to-drop-columns-by-name-in-a-data-frame); and probably more... if I tried other keywords in my search. – Joshua Ulrich Oct 06 '11 at 22:41
  • thanks! lets close this out as a duplicate. – SFun28 Oct 06 '11 at 22:44

0 Answers0