How to sort the columns horizontally largest-to-smallest by second row in list of data frames in R?

There is a list of data frames that the second row of each data frame is the total row. I want to sort the columns horizontally largest-to-smallest by total? Here is a sample of a data frame.

structure(list(PROVINCE = c("Percentage", "Total", "11", "11", 
"11", "11"), DISTRICT = c("Percentage", "Total", "1", "2", "4", 
"4"), SUB_DISTRI = c("Percentage", "Total", "21", "21", "10", 
"10"), VILLAGE = c("Percentage", "Total", "14", "5", "22", "24"
), `0` = c("0.111897382265155", "59.930000305176", "0", "9.1700000762939", 
"0", "0"), `9` = c("0.545072631055998", "291.930001258852", "0", 
"0", "9.6999998092651", "10"), `11` = c("0.0171216252074662", 
"9.1700000762939", "9.1700000762939", "0", "0", "0"), `1167` = c("0.325908361471381", 
"174.550001144409", "0", "36.6800003051756", "0", "0")), row.names = c(34L, 
33L, 1L, 2L, 3L, 4L), class = "data.frame")

I guessed that you wanted the last four columns to be numeric, so I added a line of code to so that.

library(dplyr, warn.conflicts = FALSE)
DF <- structure(list(PROVINCE = c("Percentage", "Total", "11", "11", "11", "11"), 
               DISTRICT = c("Percentage", "Total", "1", "2", "4", "4"), 
               SUB_DISTRI = c("Percentage", "Total", "21", "21", "10","10"), 
               VILLAGE = c("Percentage", "Total", "14", "5", "22", "24"), 
               `0` = c("0.111897382265155", "59.930000305176", "0", "9.1700000762939","0", "0"), 
               `9` = c("0.545072631055998", "291.930001258852", "0", "0", "9.6999998092651", "10"), 
               `11` = c("0.0171216252074662","9.1700000762939", "9.1700000762939", "0", "0", "0"), 
               `1167` = c("0.325908361471381", "174.550001144409", "0", "36.6800003051756", "0", "0")), 
          row.names = c(34L,33L, 1L, 2L, 3L, 4L), class = "data.frame")
DF <- mutate(DF, across(5:8, as.numeric)) #make the last 4 columns numeric
DF[, c("PROVINCE", "DISTRICT", "SUB_DISTRI", "VILLAGE", 
       names(sort(DF[2, 5:8], decreasing = TRUE)))]
#>     PROVINCE   DISTRICT SUB_DISTRI    VILLAGE           9        1167
#> 1 Percentage Percentage Percentage Percentage   0.5450726   0.3259084
#> 2      Total      Total      Total      Total 291.9300013 174.5500011
#> 3         11          1         21         14   0.0000000   0.0000000
#> 4         11          2         21          5   0.0000000  36.6800003
#> 5         11          4         10         22   9.6999998   0.0000000
#> 6         11          4         10         24  10.0000000   0.0000000
#>            0         11
#> 1  0.1118974 0.01712163
#> 2 59.9300003 9.17000008
#> 3  0.0000000 9.17000008
#> 4  9.1700001 0.00000000
#> 5  0.0000000 0.00000000
#> 6  0.0000000 0.00000000

Created on 2020-12-16 by the reprex package (v0.3.0)

Thank you @FJCC for your answering.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.