[R] Order of columns(variables) in dataframe
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]] __ 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.
Re: [R] Order of columns(variables) in dataframe
Is this what you want: x - read.table(textConnection( 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), header=TRUE) closeAllConnections() # order by the last row x[, order(unlist(x[5,]), decreasing=TRUE)] 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 On Sun, Jul 20, 2008 at 7:32 AM, Daniel Wagner [EMAIL PROTECTED] wrote: 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]] __ 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ 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.
Re: [R] Order of columns(variables) in dataframe
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 [EMAIL PROTECTED]: 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 __ 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.
Re: [R] Order of columns(variables) in dataframe
On 20-Jul-08 11:32:14, Daniel Wagner wrote: 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)) I wanto to get the order of df1 on the basis of value in last row (descending order) like [Sorry, had to delete your examples because of intrusive special characters, but reproduced below anyway] Could somebody help me? Daniel Amsterdam Try the following. It seems one needs to pass from dataframe to matrix, since sort() does not like lists! Maybe others know better ... 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 M-as.matrix(df1) L-M[nrow(M),] ix-sort(L,decreasing=TRUE,index.return=TRUE)$ix df2-as.data.frame(M[,ix]) df2 # 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 Hoping this helps, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 094 0861 Date: 20-Jul-08 Time: 12:52:12 -- XFMail -- __ 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.