Suppose you have this dataframe:
> dummy
index ARMA ME
1 4 2,3 -0.00764558073
2 4 3,1 -0.00017109916
3 4 3,2 -0.00038884805
4 4 3,3 -0.00246287881
5 3 1,3 -0.00004263228
6 1 0,1 0.00046965874
7 1 0,2 0.00105372919
8 1 1,0 0.00018798497
9 1 2,1 0.00112475686
10 1 2,2 -0.00121912970
11 1 3,0 0.00181957426
And the ARMA column is some kind of ID. This dataset is arranged by index from 4 to 1 (descending order)
I want to arrange the dataset by index (descending) and first the ME value closest to 0
So this would be the desired output:
> dummy
index ARMA ME
1 4 3,1 -0.00017109916
2 4 3,2 -0.00038884805
3 4 3,3 -0.00246287881
4 4 2,3 -0.00764558073
5 3 1,3 -0.00004263228
6 1 1,0 0.00018798497
7 1 0,1 0.00046965874
8 1 0,2 0.00105372919
9 1 2,1 0.00112475686
10 1 2,2 -0.00121912970
11 1 3,0 0.00181957426
Thanks in advance!!
Data:
> dput(dummy)
structure(list(index = c(4L, 4L, 4L, 4L, 3L, 1L, 1L, 1L, 1L,
1L, 1L), ARMA = c("2,3", "3,1", "3,2", "3,3", "1,3", "0,1", "0,2",
"1,0", "2,1", "2,2", "3,0"), ME = c(-0.00764558072775317, -0.000171099162494111,
-0.000388848046105807, -0.00246287880997775, -0.0000426322805130549,
0.000469658740281779, 0.001053729189345, 0.000187984966060834,
0.00112475685730634, -0.00121912969696577, 0.00181957426209449
)), row.names = c(NA, -11L), class = "data.frame")
CodePudding user response:
dummy[ order(-dummy$index, abs(dummy$ME)), ]
# index ARMA ME
# 2 4 3,1 -0.00017109916
# 3 4 3,2 -0.00038884805
# 4 4 3,3 -0.00246287881
# 1 4 2,3 -0.00764558073
# 5 3 1,3 -0.00004263228
# 8 1 1,0 0.00018798497
# 6 1 0,1 0.00046965874
# 7 1 0,2 0.00105372919
# 9 1 2,1 0.00112475686
# 10 1 2,2 -0.00121912970
# 11 1 3,0 0.00181957426
