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
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
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
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))
4 matches
Mail list logo