[R] Order of columns(variables) in dataframe

Dimitris Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Sun Jul 20 13:46:56 CEST 2008


try this:

df1 <- data.frame(v1 = c(2,3,1,9,5), v2 = c(8,5,12,4,11), v3 =  
c(7,8,2,6,9), v4 = c(1,4,6,3,6))
vals <- unlist(df1[5, ])
df1[order(vals, decreasing = TRUE)]


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://perswww.kuleuven.be/dimitris_rizopoulos/


Quoting Daniel Wagner <danieljm1976 at yahoo.com>:

> Dear R experts,
>  I have a dataframe with 4 columns (variables). I want to redorder  
>  (or reposition) these columns on the basis of a value in its last   
> row. e.g.
> Â df1<-data.frame( v1= c(2,3,1,9,5), v2=c(8,5,12,4,11),   
> v3=c(7,8,2,6,9), v4=c(1,4,6,3,6))Â Â
>> df1
> Â Â  v1 v2 v3 v4
> 1Â  2Â  8Â  7Â  1
> 2Â  3Â  5Â  8Â  4
> 3Â  1 12Â  2Â  6
> 4Â  9Â  4Â  6Â  3
> 5Â  5 11Â  9Â  6
>
> I wanto to get the order of df1 on the basis of value in last row   
> (descending order) like
> Â Â Â  v2 v3 v4 v1
> 1Â  8Â  7Â  1Â  2
> 2Â  5Â  8Â  4Â  3
> 3 12Â  2Â  6Â  1
> 4Â  4Â  6Â  3Â  9
> 5 11Â  9Â  6Â  5
>  Could somebody help me?
> Â Daniel
> Amsterdam
> Â Â Â Â Send instant messages to your online friends  
> http://uk.messenger.yahoo.com
> 	[[alternative HTML version deleted]]
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list