How to NATURAL sort a vector or data frame* by row* , in ascending order ? V1 V2 V3 V4 i1 5.000000e-01 1.036197e-17 4.825338e+16 0.00000000 i10 4.001692e-18 1.365740e-17 2.930053e-01 0.76973827 i12 -1.052843e-17 1.324484e-17 -7.949081e-01 0.42735000 i13 2.571236e-17 1.357336e-17 1.894325e+00 0.05922715 i2 -5.630739e-18 1.638267e-17 -3.437010e-01 0.73133282 i3 4.291387e-18 1.207522e-17 3.553879e-01 0.72257050 i4 1.472662e-17 1.423051e-17 1.034863e+00 0.30163897 i5 5.000000e-01 1.003323e-17 4.983441e+16 0.00000000 i6 5.147966e-18 1.569095e-17 3.280850e-01 0.74309614 i7 1.096044e-17 1.555829e-17 7.044760e-01 0.48173041 i8 -1.166290e-18 1.287370e-17 -9.059482e-02 0.92788026 i9 1.627371e-17 1.540567e-17 1.056345e+00 0.29173427 recmeanC2 9.275880e-17 6.322780e-17 1.467057e+00 0.14349903 NA NA NA NA recmeanC3 1.283534e-17 2.080644e-17 6.168929e-01 0.53781390 recmeanC4-3.079466e-17 2.565499e-17 -1.200338e+00 0.23103743 I want a sequence of rows as :-- *recmeanC2 ,recmeanC3,recmeanC4* and the *NA row in the third position from the top*(presently it's third from down) -- Thanks Moumita [[alternative HTML version deleted]]
Ottorino-Luca Pantani
2009-Aug-24 13:06 UTC
[R] natural sorting a data frame /vector by row
Moumita Das ha scritto:> How to NATURAL sort a vector or data frame* by row* , in ascending order ? > > > V1 V2 V3 V4 > i1 5.000000e-01 1.036197e-17 4.825338e+16 0.00000000 > i10 4.001692e-18 1.365740e-17 2.930053e-01 0.76973827 > i12 -1.052843e-17 1.324484e-17 -7.949081e-01 0.42735000 > i13 2.571236e-17 1.357336e-17 1.894325e+00 0.05922715 > i2 -5.630739e-18 1.638267e-17 -3.437010e-01 0.73133282 > i3 4.291387e-18 1.207522e-17 3.553879e-01 0.72257050 > i4 1.472662e-17 1.423051e-17 1.034863e+00 0.30163897 > i5 5.000000e-01 1.003323e-17 4.983441e+16 0.00000000 > i6 5.147966e-18 1.569095e-17 3.280850e-01 0.74309614 > i7 1.096044e-17 1.555829e-17 7.044760e-01 0.48173041 > i8 -1.166290e-18 1.287370e-17 -9.059482e-02 0.92788026 > i9 1.627371e-17 1.540567e-17 1.056345e+00 0.29173427 > recmeanC2 9.275880e-17 6.322780e-17 1.467057e+00 0.14349903 > NA NA NA NA > recmeanC3 1.283534e-17 2.080644e-17 6.168929e-01 0.53781390 > recmeanC4-3.079466e-17 2.565499e-17 -1.200338e+00 0.23103743 > > > > I want a sequence of rows as :-- *recmeanC2 ,recmeanC3,recmeanC4* and the *NA > row in the third position from the top*(presently it's third from down) >I do not understand what "NATURAL" stand for, but I'm not mothertongue in English. Is this the order you want ? recmeanC2 9.275880e-17 6.322780e-17 1.467057e+00 0.14349903 recmeanC3 1.283534e-17 2.080644e-17 6.168929e-01 0.53781390 recmeanC4-3.079466e-17 2.565499e-17 -1.200338e+00 0.23103743 NA NA NA NA i1 5.000000e-01 1.036197e-17 4.825338e+16 0.00000000 i2 -5.630739e-18 1.638267e-17 -3.437010e-01 0.73133282 i3 4.291387e-18 1.207522e-17 3.553879e-01 0.72257050 i4 1.472662e-17 1.423051e-17 1.034863e+00 0.30163897 i5 5.000000e-01 1.003323e-17 4.983441e+16 0.00000000 i6 5.147966e-18 1.569095e-17 3.280850e-01 0.74309614 i7 1.096044e-17 1.555829e-17 7.044760e-01 0.48173041 i8 -1.166290e-18 1.287370e-17 -9.059482e-02 0.92788026 i9 1.627371e-17 1.540567e-17 1.056345e+00 0.29173427 i10 4.001692e-18 1.365740e-17 2.930053e-01 0.76973827 ***no i11 ? *********************************************** i12 -1.052843e-17 1.324484e-17 -7.949081e-01 0.42735000 i13 2.571236e-17 1.357336e-17 1.894325e+00 0.05922715 If so I'm afraid there's no a simple way to do it. This a possible solution df.newdata <- cbind.data.frame(df.yourdata, foo=c(13, 15, 16, 17, 1, 2:12, 13:15) df.newdataOrdered <- df.newdata[sort(df.newdata$foo),] another solution could be to rename the items in column 1 -- Ottorino