anna
2010-Feb-02 16:35 UTC
[R] Deleting many columns of a data frame with the same name in a row
Hi, I have a data frame datas with half of the columns with the same name "A". I want to delete all those columns from the data frame so here is what I did: datas$A <- NULL The problem is that it deleted only one column, I would have to do it as many times as there are "A" columns. Is there a way to do it in one time? thank you ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Deleting-many-columns-of-a-data-frame-with-the-same-name-in-a-row-tp1460078p1460078.html Sent from the R help mailing list archive at Nabble.com.
Uwe Ligges
2010-Feb-02 16:44 UTC
[R] Deleting many columns of a data frame with the same name in a row
datas[ , "A" != colnames(datas)] Uwe Ligges On 02.02.2010 17:35, anna wrote:> > Hi, I have a data frame datas with half of the columns with the same name > "A". I want to delete all those columns from the data frame so here is what > I did: > datas$A<- NULL > The problem is that it deleted only one column, I would have to do it as > many times as there are "A" columns. Is there a way to do it in one time? > thank you > > ----- > Anna Lippel
anna
2010-Feb-02 16:47 UTC
[R] Deleting many columns of a data frame with the same name in a row
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(names(datas)=="A")))) but it worked ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Deleting-many-columns-of-a-data-frame-with-the-same-name-in-a-row-tp1460078p1460091.html Sent from the R help mailing list archive at Nabble.com.
Jeff Laake
2010-Feb-02 16:49 UTC
[R] Deleting many columns of a data frame with the same name in a row
Here is one way with an example: datas=data.frame(x=1:3,A=1:3,A=1:3) names(datas)=c("x","A","A") datas datas=datas[,names(datas)!="A",drop=FALSE] datas On 2/2/2010 8:35 AM, anna wrote:> Hi, I have a data frame datas with half of the columns with the same name > "A". I want to delete all those columns from the data frame so here is what > I did: > datas$A<- NULL > The problem is that it deleted only one column, I would have to do it as > many times as there are "A" columns. Is there a way to do it in one time? > thank you > > ----- > Anna Lippel >
Gabor Grothendieck
2010-Feb-02 17:18 UTC
[R] Deleting many columns of a data frame with the same name in a row
Try: newdf <- datas[names(datas) != "A"] 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(names(datas)=="A")))) > but it worked > > ----- > Anna Lippel > -- > View this message in context: http://n4.nabble.com/Deleting-many-columns-of-a-data-frame-with-the-same-name-in-a-row-tp1460078p1460091.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. >
anna
2010-Feb-02 18:23 UTC
[R] Deleting many columns of a data frame with the same name in a row
thanks this is actually shorter :) ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Deleting-many-columns-of-a-data-frame-with-the-same-name-in-a-row-tp1460078p1460208.html Sent from the R help mailing list archive at Nabble.com.