[R] Deleting a column in a dataframe by name

Jason Rupert jasonkrupert at yahoo.com
Wed Sep 30 04:47:21 CEST 2009


What about using NULL?

> x<-runif(12)
> y<-runif(12)
> z<-runif(12)
> mydf<-data.frame(cbind(x,y,z))
> names(mydf)
[1] "x" "y" "z"
> mydf$x<-NULL
> names(mydf)
[1] "y" "z"



--- On Tue, 9/29/09, Rolf Turner <r.turner at auckland.ac.nz> wrote:

> From: Rolf Turner <r.turner at auckland.ac.nz>
> Subject: Re: [R] Deleting a column in a dataframe by name
> To: "Duncan Murdoch" <murdoch at stats.uwo.ca>
> Cc: "Dennis Fisher" <fisher at plessthan.com>, "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
> Date: Tuesday, September 29, 2009, 4:36 PM
> 
> On 30/09/2009, at 10:25 AM, Duncan Murdoch wrote:
> 
> > On 29/09/2009 4:54 PM, Rolf Turner wrote:
> >> On 30/09/2009, at 9:32 AM, milton ruser wrote:
> >>
> >>> x=runif(12)
> >>> y=runif(12)
> >>> w=runif(12)
> >>>
> >>> mydf<-data.frame(cbind(x,y,w))
> >>> head(mydf)
> >>>
> >>> mydf<-subset(mydf, select=c(-x,-w))
> >>> head(mydf)
> >>>
> >>>
> >>     But this doesn't work if NAME1
> and NAME2 are ***names***,
> >>     as the terminology would
> apply.  And that is precisely the
> >>     situation in which one would
> wish to apply this sort of
> >>     technique.
> >
> > I'm not completely sure what you're getting at here,
> but I think  
> > milton
> > is right in this example.  (The original one was
> missing the "c".)
> 
>     milton is right if you explicitly type
> in the names of the
>     columns to be deleted.  But it
> doesn't work if you store
>     the names of the columns as character
> strings.  That's what
>     you would have to do when writing code
> to handle complex
>     situations where you didn't know that
> column names in advance.
> 
>     And if you're not writing code, well why
> not just do something
>     simple like mydf[,2] ???
> >
> > subset() is weird, it does very strange stuff in the
> select argument.
> 
> 
>     Truer words were never spoken/written.
> 
>         cheers,
> 
>            
> Rolf
> 
> ######################################################################
> Attention:\ This e-mail message is privileged and
> confid...{{dropped:9}}
> 
> ______________________________________________
> 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