Dimitri Shvorob
2010-Jan-24 11:58 UTC
[R] Is there a quicker way to drop a data frame column than setting it to NULL?
If I want to drop columns x, y, z from dataframe df, is there a better alternative to df$x = NULL df$y = NULL df$z = NULL There are sufficiently many columns remaining to make df = subset(df, select = c(a,b,c,d[etc])) cumbersome. Thank you. -- View this message in context: http://n4.nabble.com/Is-there-a-quicker-way-to-drop-a-data-frame-column-than-setting-it-to-NULL-tp1288617p1288617.html Sent from the R help mailing list archive at Nabble.com.
Peter Ehlers
2010-Jan-24 13:11 UTC
[R] Is there a quicker way to drop a data frame column than setting it to NULL?
Dimitri Shvorob wrote:> If I want to drop columns x, y, z from dataframe df, is there a better > alternative to > > df$x = NULL > df$y = NULL > df$z = NULL > > There are sufficiently many columns remaining to make > > df = subset(df, select = c(a,b,c,d[etc])) > > cumbersome.You can use subset with the select argument specifying which variables to omit: df = subset(df, select = !names(df) %in% c('x', 'y', 'z')) or just df[!names(df) %in% c('x', 'y', 'z')] -Peter Ehlers> > Thank you.-- Peter Ehlers University of Calgary
Gabor Grothendieck
2010-Jan-24 13:26 UTC
[R] Is there a quicker way to drop a data frame column than setting it to NULL?
Try: subset(df, select = - c(x, y, z)) On Sun, Jan 24, 2010 at 6:58 AM, Dimitri Shvorob <dimitri.shvorob at gmail.com> wrote:> > If I want to drop columns x, y, z from dataframe df, is there a better > alternative to > > df$x = NULL > df$y = NULL > df$z = NULL > > There are sufficiently many columns remaining to make > > df = subset(df, select = c(a,b,c,d[etc])) > > cumbersome. > > Thank you. > -- > View this message in context: http://n4.nabble.com/Is-there-a-quicker-way-to-drop-a-data-frame-column-than-setting-it-to-NULL-tp1288617p1288617.html > Sent from the R help mailing list archive at Nabble.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. >
Carlos J. Gil Bellosta
2010-Jan-24 19:02 UTC
[R] Is there a quicker way to drop a data frame column than setting it to NULL?
Hello, You can always use df <- subset( df, select = -c(x, y, z) ) Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com Dimitri Shvorob wrote:> If I want to drop columns x, y, z from dataframe df, is there a better > alternative to > > df$x = NULL > df$y = NULL > df$z = NULL > > There are sufficiently many columns remaining to make > > df = subset(df, select = c(a,b,c,d[etc])) > > cumbersome. > > Thank you.