Hi, I have a 1-column dataframe with rownames and I want to sort it based on the single column. The typical procedure that is recommended in diverse posts is to use order in the index. But that "destroys" my dataframe structure. Probabaly it is a very simple solution. Here is a short reproducable example: x <- c(1,3,51,2,34,44,12,33,2,8) df <- data.frame(x) rownames(df) <- c("A","B","C","D","E","F","G","H","I","J") df.sort <- df[order(df[,"x"]),] /Johannes -- Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
On Fri, 8 Jun 2012, Johannes Radinger wrote:> Hi, > > I have a 1-column dataframe with rownames and I want to sort it > based on the single column. The typical procedure that is recommended > in diverse posts is to use order in the index. But that "destroys" my > dataframe structure. Probabaly it is a very simple solution. Here is a > short reproducable example: > > x <- c(1,3,51,2,34,44,12,33,2,8) > > df <- data.frame(x) > rownames(df) <- c("A","B","C","D","E","F","G","H","I","J") > > df.sort <- df[order(df[,"x"]),]If you select a single column, then the default is to drop the data.frame property. You exploit this in your own code in df[,"x"] which yields a simple vector. To suppress this behavior you can set drop = FALSE: df.sort <- df[order(df[,"x"]), , drop = FALSE] hth, Z> /Johannes > -- > > Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a > > ______________________________________________ > 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. >
Hi Johannes, You need to add the drop=FALSE argument in order to keep the structure: df.sort <- df[order(df[,"x"]), , drop=FALSE] ## don't forget the extra comma See ?"[" for details HTH, Ivan -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS/uB 6282 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE +33(0)3.80.39.63.06 ivan.calandra at u-bourgogne.fr http://biogeosciences.u-bourgogne.fr/calandra Le 08/06/12 09:22, Johannes Radinger a ?crit : Hi, I have a 1-column dataframe with rownames and I want to sort it based on the single column. The typical procedure that is recommended in diverse posts is to use order in the index. But that "destroys" my dataframe structure. Probabaly it is a very simple solution. Here is a short reproducable example: x<- c(1,3,51,2,34,44,12,33,2,8) df<- data.frame(x) rownames(df)<- c("A","B","C","D","E","F","G","H","I","J") df.sort<- df[order(df[,"x"]),] /Johannes
Hi Achim, thank you for your good explanation and the solution to my question... cheers, /j -------- Original-Nachricht --------> Datum: Fri, 8 Jun 2012 09:30:42 +0200 (CEST) > Von: Achim Zeileis <Achim.Zeileis at uibk.ac.at> > An: Johannes Radinger <JRadinger at gmx.at> > CC: R-help at r-project.org > Betreff: Re: [R] Sort 1-column dataframe with rownames> On Fri, 8 Jun 2012, Johannes Radinger wrote: > > > Hi, > > > > I have a 1-column dataframe with rownames and I want to sort it > > based on the single column. The typical procedure that is recommended > > in diverse posts is to use order in the index. But that "destroys" my > > dataframe structure. Probabaly it is a very simple solution. Here is a > > short reproducable example: > > > > x <- c(1,3,51,2,34,44,12,33,2,8) > > > > df <- data.frame(x) > > rownames(df) <- c("A","B","C","D","E","F","G","H","I","J") > > > > df.sort <- df[order(df[,"x"]),] > > If you select a single column, then the default is to drop the data.frame > property. You exploit this in your own code in df[,"x"] which yields a > simple vector. To suppress this behavior you can set drop = FALSE: > > df.sort <- df[order(df[,"x"]), , drop = FALSE] > > hth, > Z > > > /Johannes > > -- > > > > Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a > > > > ______________________________________________ > > 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. > >-- Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
Hi, Try this, df.sort<-apply(df,2,sort)> df.sort?? x A? 1 D? 2 I? 2 B? 3 J? 8 G 12 H 33 E 34 F 44 C 51 df.sort<-as.data.frame(df.sort) A.K. ----- Original Message ----- From: Johannes Radinger <JRadinger at gmx.at> To: R-help at r-project.org Cc: Sent: Friday, June 8, 2012 3:22 AM Subject: [R] Sort 1-column dataframe with rownames Hi, I have a 1-column dataframe with rownames and I want to sort it based on the single column. The typical procedure that is recommended in diverse posts is to use order in the index. But that "destroys" my dataframe structure. Probabaly it is a very simple solution. Here is a short reproducable example: x <- c(1,3,51,2,34,44,12,33,2,8) df <- data.frame(x) rownames(df) <- c("A","B","C","D","E","F","G","H","I","J") df.sort <- df[order(df[,"x"]),] /Johannes -- Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a ______________________________________________ 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.