[R] Deleting many columns of a data frame with the same name ina row
William Dunlap
wdunlap at tibco.com
Tue Feb 2 19:11:54 CET 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Steve Lianoglou
> Sent: Tuesday, February 02, 2010 9:05 AM
> To: anna
> Cc: r-help at r-project.org
> Subject: Re: [R] Deleting many columns of a data frame with
> the same name ina row
>
> Hi,
>
> On Tue, Feb 2, 2010 at 11:47 AM, anna
> <lippelanna24 at hotmail.com> wrote:
> >
> > This is what I just found now but I guess there is a simpler way:
> >
> >
> datas[which(names(datas)=="A")]<-list(rep(NULL,length(which(na
mes(datas)=="A"))))
> > but it worked
>
> For what it's worth, you could also have done:
>
> clean <- datas[,-which(names(datas)=="A")]
>
> (note that indexing with a "negative" vector removes those
> rows/columns from your object (instead of picking them)).
I would recommend replacing
datas[, -which(names(data)=="A")]
with
datas[, names(data)!="A"]
because the former returns a zero-column data.frame if there
are columns of datas named "A". (which(rep(F,n))->integer(0)
and -integer(0) is identical to integer(0), which selects
no elements). The latter returns the entire data.frame in
the case.
Logical subscripts are your friend.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
> -steve
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
> | Memorial Sloan-Kettering Cancer Center
> | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>
> ______________________________________________
> 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