Hi all, Probably a simple question, but I just can't find a simple answear in the older threads or anywhere else. I've added some new vectors as columns in a data frame using cbind(). As they're all put as the last columns inte the data frame, I would like to move them to specific positions. How do you do to change the position of a column in a data frame? I know I can use fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create a new data frame with the given columns in the specified order, but there must be an easier way..? All the best, Joel _________________________________________________________________ Nya Windows 7 - Hitta en dator som passar dig! Mer information. http://windows.microsoft.com/shop [[alternative HTML version deleted]]
dataframe xx x1 x2 x3 1 2 5 2 4 1 5 6 0 1 1 2 data.frame(xx$x2,xx$x1,xx$x3) # or Awkward but works --- On Fri, 10/23/09, Joel F?rstenberg-H?gg <joel_furstenberg_hagg at hotmail.com> wrote:> From: Joel F?rstenberg-H?gg <joel_furstenberg_hagg at hotmail.com> > Subject: [R] Change positions of columns in data frame > To: r-help at r-project.org > Received: Friday, October 23, 2009, 10:19 AM > > Hi all, > > Probably a simple question, but I just can't find a simple > answear in the older threads or anywhere else. > > I've added some new vectors as columns in a data frame > using cbind(). As they're all put as the last columns inte > the data frame, I would like to move them to specific > positions. How do you do to change the position of a column > in a data frame? > > I know I can use > fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), > Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create > a new data frame with the given columns in the specified > order, but there must be an easier way..? > > All the best, > > Joel > ??? > ???????? > ?????? ??? > ? > _________________________________________________________________ > Nya Windows 7 - Hitta en dator som passar dig! Mer > information. > http://windows.microsoft.com/shop > ??? [[alternative HTML version deleted]] > > ______________________________________________ > 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. >__________________________________________________________________ Be smarter than spam. See how smart SpamGuard is at giving ju
DF[ ,names(DF)[c(your_preferred_order)]] -Peter Ehlers Joel F?rstenberg-H?gg wrote:> Hi all, > > Probably a simple question, but I just can't find a simple answear in the older threads or anywhere else. > > I've added some new vectors as columns in a data frame using cbind(). As they're all put as the last columns inte the data frame, I would like to move them to specific positions. How do you do to change the position of a column in a data frame? > > I know I can use fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create a new data frame with the given columns in the specified order, but there must be an easier way..? > > All the best, > > Joel > > _________________________________________________________________ > Nya Windows 7 - Hitta en dator som passar dig! Mer information. > http://windows.microsoft.com/shop > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >
Joel - Suppose the columns are named x1, x2, x3, x4, and x5. You can use subscripting: x[c('x2','x4','x1','x3','x5')] or, to save typing the quotes subset(x,select=c(x2,x4,x1,x3,x5)) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 23 Oct 2009, Joel F?rstenberg-H?gg wrote:> > Hi all, > > Probably a simple question, but I just can't find a simple answear in the older threads or anywhere else. > > I've added some new vectors as columns in a data frame using cbind(). As they're all put as the last columns inte the data frame, I would like to move them to specific positions. How do you do to change the position of a column in a data frame? > > I know I can use fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create a new data frame with the given columns in the specified order, but there must be an easier way..? > > All the best, > > Joel > > _________________________________________________________________ > Nya Windows 7 - Hitta en dator som passar dig! Mer information. > http://windows.microsoft.com/shop > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
You can change the order of nearly everything just by applying an appropriate index and assigning to the original object. Something like data <- data[,order_of_columns] Tom Joel F?rstenberg-H?gg schrieb:> Hi all, > > Probably a simple question, but I just can't find a simple answear in the older threads or anywhere else. > > I've added some new vectors as columns in a data frame using cbind(). As they're all put as the last columns inte the data frame, I would like to move them to specific positions. How do you do to change the position of a column in a data frame? > > I know I can use fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create a new data frame with the given columns in the specified order, but there must be an easier way..? > > All the best, > > Joel > > _________________________________________________________________ > Nya Windows 7 - Hitta en dator som passar dig! Mer information. > http://windows.microsoft.com/shop > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Technische Universit?t M?nchen Department f?r Pflanzenwissenschaften Lehrstuhl f?r Gr?nlandlehre Am Hochanger 1 85350 Freising / Germany Phone: ++49 (0)8161 715324 Fax: ++49 (0)8161 713243 email: tom.gottfried at wzw.tum.de http://www.wzw.tum.de/gruenland
Hi Joel, The answers you've received already, suggesting subscripting, are good because they strengthen your understanding of R subscripting. However, sometimes these methods produce "strange" column names. So, what I usually do is use the subset command. You don't have to provide anything for the subset argument (i.e., you'll keep all the rows) but you can re-order the columns by listing them in the order you want, within the select argument, like this DF<-subset(DF,select=c(var3,var2,var1)) HTH, Mark 2009/10/23 Joel Fürstenberg-Hägg <joel_furstenberg_hagg@hotmail.com>> > Hi all, > > Probably a simple question, but I just can't find a simple answear in the > older threads or anywhere else. > > I've added some new vectors as columns in a data frame using cbind(). As > they're all put as the last columns inte the data frame, I would like to > move them to specific positions. How do you do to change the position of a > column in a data frame? > > I know I can use > fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), > Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create a new data frame > with the given columns in the specified order, but there must be an easier > way..? > > All the best, > > Joel > > _________________________________________________________________ > Nya Windows 7 - Hitta en dator som passar dig! Mer information. > http://windows.microsoft.com/shop > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Mark Na University of Saskatchewan Saskatoon, Canada [[alternative HTML version deleted]]