Re: [R] inserting columns in the middle of a dataframe

From: Berton Gunter <gunter.berton_at_gene.com>
Date: Wed 13 Sep 2006 - 22:32:37 GMT


Please folks -- use indexing.

myframe<-myframe[,c(1,5,2,3,4)]

Which begs the question: why bother rearranging the columns anyway, since one can get them used, printed, etc. in any order you wish anytime you want just by specifying the indices in the order you want them. I suspect the question was motivated by too much Sas- or Excel -ism.

"The business of the statistician is to catalyze the scientific learning process." - George E. P. Box    

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Timothy Bates
> Sent: Wednesday, September 13, 2006 3:05 PM
> To: Jon Minton; r-help@stat.math.ethz.ch
> Subject: Re: [R] inserting columns in the middle of a dataframe
>
>
> > Is there a built-in and simple way to insert new columns in
> a dataframe?
>
> You do this by collecting the columns in the new order you desire, and
> making a new frame.
>
> oldframe <- data.frame(matrix(0:14,ncol=3))
> newcol <- data.frame(20:24)
> names(newcol) <- "newcol"
> newframe <- data.frame(c(oldframe[1],newcol, oldframe[2:3]))
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>



R-help@stat.math.ethz.ch 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. Received on Thu Sep 14 08:36:06 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 29 Sep 2006 - 03:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.