[R] Reshape dataframe according to ordered variables

Duarte Viana viana.sptd at gmail.com
Thu Mar 18 16:27:27 CET 2010


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



More information about the R-help mailing list