Dear all, I am still a R apprentice... Apologies for the basic question. I am trying to reshape a dataframe based on the order of two variables (a character variable and a numerical variable). To simplify it, consider the following dataframe> df<-data.frame(id=c("b","b","a","a","a"),ord=c(2,1,1,3,2))id ord 1 b 2 2 b 1 3 a 1 4 a 3 5 a 2 I want to reshape it such that it results in this dataframe ("id" alphabetically ordered and ranked according to the "ord" order) df1<-data.frame(id=c("a","a","a","b","b"),ord=c(1,2,3,1,2)) id ord 1 a 1 2 a 2 3 a 3 4 b 1 5 b 2 Thanks in advance, Duarte Viana
try this:> dfid ord 1 b 2 2 b 1 3 a 1 4 a 3 5 a 2> df[order(df$id, df$ord),]id ord 3 a 1 5 a 2 4 a 3 2 b 1 1 b 2>On Thu, Mar 18, 2010 at 11:27 AM, Duarte Viana <viana.sptd@gmail.com> wrote:> Dear all, > > I am still a R apprentice... Apologies for the basic question. > I am trying to reshape a dataframe based on the order of two variables > (a character variable and a numerical variable). To simplify it, > consider the following dataframe > > > df<-data.frame(id=c("b","b","a","a","a"),ord=c(2,1,1,3,2)) > > id ord > 1 b 2 > 2 b 1 > 3 a 1 > 4 a 3 > 5 a 2 > > I want to reshape it such that it results in this dataframe ("id" > alphabetically ordered and ranked according to the "ord" order) > > df1<-data.frame(id=c("a","a","a","b","b"),ord=c(1,2,3,1,2)) > > id ord > 1 a 1 > 2 a 2 > 3 a 3 > 4 b 1 > 5 b 2 > > Thanks in advance, > > Duarte Viana > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
MORNEAU François
2010-Mar-18 15:36 UTC
[R] Reshape dataframe according to ordered variables
Hello Duarte, It seems that "order" is what you are looking for : df <- df[order(df$id, df$ord), ] Regards, Fran?ois -----Message d'origine----- De?: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] De la part de Duarte Viana Envoy??: jeudi 18 mars 2010 16:27 ??: r-help Objet?: [R] Reshape dataframe according to ordered variables Dear all, I am still a R apprentice... Apologies for the basic question. I am trying to reshape a dataframe based on the order of two variables (a character variable and a numerical variable). To simplify it, consider the following dataframe> df<-data.frame(id=c("b","b","a","a","a"),ord=c(2,1,1,3,2))id ord 1 b 2 2 b 1 3 a 1 4 a 3 5 a 2 I want to reshape it such that it results in this dataframe ("id" alphabetically ordered and ranked according to the "ord" order) df1<-data.frame(id=c("a","a","a","b","b"),ord=c(1,2,3,1,2)) id ord 1 a 1 2 a 2 3 a 3 4 b 1 5 b 2 Thanks in advance, Duarte Viana ______________________________________________ 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.