0

Assume the following data frame df

  k    final01    final12     final11     final10     final13     final14    final15
1 0 0.01132197 0.01242985 0.003420944 0.003734922 0.004676629 0.003233194 0.02645520
2 1 0.02945764 0.02462278 0.008050575 0.008931167 0.006695612 0.004657732 0.03373639
3 2 0.02802845 0.02486389 0.006674203 0.006913728 0.006871522 0.004799810 0.02929527
4 3 0.02753059 0.02500377 0.006830818 0.006857118 0.006937120 0.004930983 0.02933087
5 4 0.02739441 0.02474920 0.006954684 0.006649320 0.006512953 0.004712823 0.02819380
6 5 0.02782208 0.02465878          NA          NA          NA          NA         NA

Here the dput(df)

structure(list(k = 0:5, final01 = c(0.0113219652566949, 0.0294576400488878, 
0.0280284464532738, 0.0275305922885969, 0.0273944087059707,0.0278220846696326
), final10 = c(0.0124298469345964, 0.024622784463692, 0.0248638940062316, 
0.025003765032319, 0.0247492039902097, 0.0246587782824302), final11 = c(0.00342094396763939, 
0.00805057453957241, 0.00667420265709246, 0.00683081837187657, 
0.00695468361107997, NA), final12 = c(0.00373492219945338, 0.00893116747327287, 
0.00691372841938441, 0.00685711798770184, 0.00664932025914572, 
NA), final13 = c(0.00467662856293965, 0.00669561244661012, 0.00687152189408537, 
0.00693712042565242, 0.00651295285883445, NA), final14 = c(0.00323319390635929, 
0.00465773181030785, 0.00479980993663797, 0.00493098278746824, 
0.00471282267245847, NA), final15 = c(0.0264552040924062, 0.0337363880196297, 
0.0292952726979338, 0.0293308732480174, 0.0281937993522965, NA
)), .Names = c("k", "final01", "final10", "final11", "final12", 
"final13", "final14", "final15"), class = "data.frame", row.names = c(NA, 
6L))

I want to reorder the my dataframe like this:

   k final01 final10 final11 final12 final13 final14 final15
1  0
2  1
3  2
4  3
5  4
6  5

This is just a short example. If it helps: My dataframe contains columns from final01-final20

zx8754
  • 52,746
  • 12
  • 114
  • 209
Leo96
  • 489
  • 3
  • 12
  • 1
    you can use `order(as.numeric(sub("final", "", colnames(df))))` or have a look at package `gtools` for `mixedsort` (though you'll need the "order" version of that) – Cath Apr 05 '18 at 12:57
  • 1
    Or alternatively maybe `df[,sort(colnames(df))]`? – Florian Apr 05 '18 at 12:58
  • 1
    @Florian `mixedsort` ;-) – Cath Apr 05 '18 at 12:58
  • 1
    Oops, I did not see there is a column 'k'. Maybe rename that to 'e', haha – Florian Apr 05 '18 at 12:59
  • 1
    so, to sum up, you can do `df[, c("k", gtools::mixedsort(colnames(df)[-1]))]` – Cath Apr 05 '18 at 13:03
  • I dont have acess to all packages. I cant get gtools. So I'm trying with base r. I already tried order(as.numeric(sub("final", "", colnames(df)))) or df[,sort(colnames(df))]. Here is the ordering of output from my real data: order(as.numeric(sub("final", "", colnames(compare_error)[-1]))) 1 12 14 15 16 17 18 19 20 2 3 4 5 6 7 8 9 10 11 13 and the resulted order of the data frame k final19 final20 final03 final04 final05 final06 final07 final08 final01 inal10 final11 final12 final13 final14 final15 final16 final17 final18 final02 – Leo96 Apr 05 '18 at 13:10
  • 2
    `df[, c(1, order(as.numeric(sub("final", "", colnames(df)[-1])))+1)]` – Cath Apr 05 '18 at 13:11
  • Ah, that helps. Thanks a lot ! – Leo96 Apr 05 '18 at 13:14

0 Answers0