Reputation: 463
I would like to order numbers in column Rn from the smallest to the highest in my data frame.
df
Rn A B
999 999 -0.0041211884 1.525399e-02
998 998 -0.0029667159 -1.210880e-02
997 997 -0.0021222138 -8.990619e-04
996 996 -0.0034225292 6.550007e-03
995 995 -0.0022323718 4.861625e-03
734 734 -0.0031919646 0.0183762923
733 733 -0.0041087604 0.0142102064
732 732 -0.0026854019 0.0133951684
731 731 -0.0041045443 -0.0357000188
730 730 -0.0019459701 0.0046243762
73 73 -0.0006597477 -0.00419513
729 729 -0.0041603061 0.0249331809
728 728 -0.0028535264 0.0096861556
this doesn't work
df1 <- df[order(df$Rn),]
It gives this weird output where beginning looks fine but in lower rows you get much smaller values.
1 1 0.0001128847 -0.0175509661
2 10 -0.0040341430 0.0228551849
3 100 -0.0036553401 0.0170472191
4 1000 -0.0039037856 0.0217404106
5 1001 -0.0044555778 0.0251330163
6 1002 -0.0037489438 0.0222520941
7 1003 -0.0042961918 0.0227658211
8 1004 -0.0042094921 -0.0049233698
9 1005 -0.0033948795 0.0188741941
10 1006 -0.0047154595 0.0132463085
11 1007 -0.0034839550 0.0170066750
12 1008 -0.0051087864 -0.0043333614
13 1009 -0.0031065428 0.0164741372
14 101 -0.0029574478 0.0006646753
Upvotes: 0
Views: 3094
Reputation: 887711
We can use order
df1 <- df[order(as.numeric(as.character(df$Rn))),]
If the values to be ordered are limited to a single column
df$Rn <- sort(df$Rn)
Or with arrange
library(dplyr)
df %>%
arrange(as.numeric(as.character(Rn)))
# Rn A B
#72 72 -0.0044245791 -0.0168038397
#73 73 -0.0006597477 -0.0041951300
#720 720 -0.0040865789 0.0126819770
#721 721 -0.0025343630 0.0154996377
#722 722 -0.0045191481 0.0222403429
#723 723 -0.0037375573 0.0208354495
#724 724 -0.0016881401 -0.0010435999
#725 725 -0.0011054237 -0.0350180254
#726 726 -0.0017715684 -0.0332047624
#727 727 -0.0028488675 0.0121699256
#728 728 -0.0028535264 0.0096861556
#729 729 -0.0041603061 0.0249331809
#730 730 -0.0019459701 0.0046243762
#731 731 -0.0041045443 -0.0357000188
#732 732 -0.0026854019 0.0133951684
#733 733 -0.0041087604 0.0142102064
#734 734 -0.0031919646 0.0183762923
#995 995 -0.0022323718 0.0048616250
#996 996 -0.0034225292 0.0065500070
#997 997 -0.0021222138 -0.0008990619
#998 998 -0.0029667159 -0.0121088000
#999 999 -0.0041211884 0.0152539900
df <- structure(list(Rn = c(999L, 998L, 997L, 996L, 995L, 734L, 733L,
732L, 731L, 730L, 73L, 729L, 728L, 727L, 726L, 725L, 724L, 723L,
722L, 721L, 720L, 72L), A = c(-0.0041211884, -0.0029667159, -0.0021222138,
-0.0034225292, -0.0022323718, -0.0031919646, -0.0041087604, -0.0026854019,
-0.0041045443, -0.0019459701, -0.0006597477, -0.0041603061, -0.0028535264,
-0.0028488675, -0.0017715684, -0.0011054237, -0.0016881401, -0.0037375573,
-0.0045191481, -0.002534363, -0.0040865789, -0.0044245791), B = c(0.01525399,
-0.0121088, -0.0008990619, 0.006550007, 0.004861625, 0.0183762923,
0.0142102064, 0.0133951684, -0.0357000188, 0.0046243762, -0.00419513,
0.0249331809, 0.0096861556, 0.0121699256, -0.0332047624, -0.0350180254,
-0.0010435999, 0.0208354495, 0.0222403429, 0.0154996377, 0.012681977,
-0.0168038397)), class = "data.frame", row.names = c("999", "998",
"997", "996", "995", "734", "733", "732", "731", "730", "73",
"729", "728", "727", "726", "725", "724", "723", "722", "721",
"720", "72"))
Upvotes: 3