Home > Software design >  Sorting a column in a data frame in R
Sorting a column in a data frame in R

Time:06-18

*I wanted to arrange the column "TotalConfirmedCases" in descending order but it sorted in a weird way like 965 is arranged first.

CODE in R: new_Cor_table[rev(order(new_Cor_table$TotalConfirmedCases)),]

Output:

enter image description here

CodePudding user response:

I suggest using the rank function, with a negative sign it will reverse the order

new_Cor_table[order (-rank (new_Cor_table$TotalConfirmedCases)),]

CodePudding user response:

Update: thanks to input of @onyambu: We could use order with decreasing=TRUE:

newdata <- df[order(df$TotalConfirmedCases, decreasing = TRUE),] 

OR

If we want to do it with rev then here is the syntax:

newdata <- df[rev(order(df$TotalConfirmedCases)),]   
newdata

              County TotalConfirmedCases Totalprobablecases Totalcases Totaldeaths
3             Dakota               95277             23,252    118,529         792
7              Anoka               83623             20,459    104,082         808
26        Washington               57910             14,193     72,103         490
30           Stearns               50672              2,622     53,294         372
34           Olmsted               44718              1,048     45,766         191
36         St. Louis               43103              8,153     51,256         541
2            Douglas                9534              1,962     11,496         118
5             Isanti                8892              1,645     10,537         119
4           Morrison                8892                616      9,508         105
6           Freeborn                8753                679      9,432          77
8           Nicollet                8244                385      8,629          66
9             Becker                7877              1,292      9,169          95
11              Polk                7319              1,852      9,171         109
12           Carlton                7203              2,451      9,654         100
13        Mille Lacs                6962                578      7,540         116
15              Cass                6687                668      7,355          83
16              Todd                6605                486      7,091          61
17              Lyon                6503                759      7,262          74
18             Brown                6460                330      6,790          81
19          Le Sueur                6294                449      6,743          51
21              Pine                6141              1,319      7,460          68
22            Nobles                6025              1,044      7,069          60
23             Dodge                5916                144      6,060          22
24            Meeker                5803                361      6,164          75
25           Wabasha                5795                172      5,967          19
28            Waseca                5314                424      5,738          39
29            Martin                5273                549      5,822          65
31          Fillmore                4953                117      5,070          24
32           Hubbard                4579                556      5,135          60
33           Houston                4498                320      4,818          20
35            Roseau                4327                281      4,608          45
37         Faribault                3759                213      3,972          54
38           Redwood                3661                417      4,078          54
39            Wadena                3636                754      4,390          56
1            Kittson                 965                109      1,074          28
10 Lake\tof the Woods                 771                 34        805           6
14          Red Lake                 692                269        961          13
20              Cook                 620                 12        632           4
27          Traverse                 577                313        890          10
> 

data:

structure(list(County = c("Kittson", "Douglas", "Dakota", "Morrison", 
"Isanti", "Freeborn", "Anoka", "Nicollet", "Becker", "Lake\tof the Woods", 
"Polk", "Carlton", "Mille Lacs", "Red Lake", "Cass", "Todd", 
"Lyon", "Brown", "Le Sueur", "Cook", "Pine", "Nobles", "Dodge", 
"Meeker", "Wabasha", "Washington", "Traverse", "Waseca", "Martin", 
"Stearns", "Fillmore", "Hubbard", "Houston", "Olmsted", "Roseau", 
"St. Louis", "Faribault", "Redwood", "Wadena"), TotalConfirmedCases = c(965L, 
9534L, 95277L, 8892L, 8892L, 8753L, 83623L, 8244L, 7877L, 771L, 
7319L, 7203L, 6962L, 692L, 6687L, 6605L, 6503L, 6460L, 6294L, 
620L, 6141L, 6025L, 5916L, 5803L, 5795L, 57910L, 577L, 5314L, 
5273L, 50672L, 4953L, 4579L, 4498L, 44718L, 4327L, 43103L, 3759L, 
3661L, 3636L), Totalprobablecases = c("109", "1,962", "23,252", 
"616", "1,645", "679", "20,459", "385", "1,292", "34", "1,852", 
"2,451", "578", "269", "668", "486", "759", "330", "449", "12", 
"1,319", "1,044", "144", "361", "172", "14,193", "313", "424", 
"549", "2,622", "117", "556", "320", "1,048", "281", "8,153", 
"213", "417", "754"), Totalcases = c("1,074", "11,496", "118,529", 
"9,508", "10,537", "9,432", "104,082", "8,629", "9,169", "805", 
"9,171", "9,654", "7,540", "961", "7,355", "7,091", "7,262", 
"6,790", "6,743", "632", "7,460", "7,069", "6,060", "6,164", 
"5,967", "72,103", "890", "5,738", "5,822", "53,294", "5,070", 
"5,135", "4,818", "45,766", "4,608", "51,256", "3,972", "4,078", 
"4,390"), Totaldeaths = c(28L, 118L, 792L, 105L, 119L, 77L, 808L, 
66L, 95L, 6L, 109L, 100L, 116L, 13L, 83L, 61L, 74L, 81L, 51L, 
4L, 68L, 60L, 22L, 75L, 19L, 490L, 10L, 39L, 65L, 372L, 24L, 
60L, 20L, 191L, 45L, 541L, 54L, 54L, 56L)), class = "data.frame", row.names = c(NA, 
-39L))
  • Related