I am trying to expand on this answer, by creating a solution that works both on the new_dat
and the old_dat
.
New Data
new_dat <- structure(list(`[0,25) east` = c(1269L, 85L), `[0,25) north` = c(364L,
21L), `[0,25) south` = c(1172L, 97L), `[0,25) west` = c(549L,
49L), `[100,250) east` = c(441L, 149L), `[100,250) north` = c(224L,
45L), `[100,250) south` = c(521L, 247L), `[100,250) west` = c(770L,
124L), `[100,500) east` = c(0L, 0L), `[100,500) north` = c(0L,
0L), `[100,500) south` = c(0L, 0L), `[100,500) west` = c(0L,
0L), `[1000,1000000] east` = c(53L, 0L), `[1000,1000000] north` = c(82L,
0L), `[1000,1000000] south` = c(23L, 0L), `[1000,1000000] west` = c(63L,
0L), `[1000,1500) east` = c(0L, 0L), `[1000,1500) north` = c(0L,
0L), `[1000,1500) south` = c(0L, 0L), `[1000,1500) west` = c(0L,
0L), `[1500,3000) east` = c(0L, 0L), `[1500,3000) north` = c(0L,
0L), `[1500,3000) south` = c(0L, 0L), `[1500,3000) west` = c(0L,
0L), `[25,100) east` = c(579L, 220L), `[25,100) north` = c(406L,
58L), `[25,100) south` = c(1048L, 316L), `[25,100) west` = c(764L,
131L), `[25,50) east` = c(0L, 0L), `[25,50) north` = c(0L, 0L
), `[25,50) south` = c(0L, 0L), `[25,50) west` = c(0L, 0L), `[250,500) east` = c(232L,
172L), `[250,500) north` = c(207L, 40L), `[250,500) south` = c(202L,
148L), `[250,500) west` = c(457L, 153L), `[3000,1000000] east` = c(0L,
0L), `[3000,1000000] north` = c(0L, 0L), `[3000,1000000] south` = c(0L,
0L), `[3000,1000000] west` = c(0L, 0L), `[50,100) east` = c(0L,
0L), `[50,100) north` = c(0L, 0L), `[50,100) south` = c(0L, 0L
), `[50,100) west` = c(0L, 0L), `[500,1000) east` = c(103L, 0L
), `[500,1000) north` = c(185L, 0L), `[500,1000) south` = c(66L,
0L), `[500,1000) west` = c(200L, 0L), `[500,1000000] east` = c(0L,
288L), `[500,1000000] north` = c(0L, 120L), `[500,1000000] south` = c(0L,
229L), `[500,1000000] west` = c(0L, 175L)), row.names = c("A",
"B"), class = "data.frame")
Old data and original Solution
old_dat <- structure(list(`[0,25)` = 5L, `[100,250)` = 43L, `[100,500)` = 0L,
`[1000,1000000]` = 20L, `[1000,1500)` = 0L, `[1500,3000)` = 0L,
`[25,100)` = 38L, `[25,50)` = 0L, `[250,500)` = 27L, `[3000,1000000]` = 0L,
`[50,100)` = 0L, `[500,1000)` = 44L, `[500,1000000]` = 0L), row.names = "Type_A", class = "data.frame")
The solution makes uses of the fact that the sum of the two numbers in each column name added provide the correct order.
ord <- gsub("\\[|\\]|\\)", "", colnames(new_dat)) %>%
strsplit(",") %>%
lapply(as.numeric) %>%
lapply(sum) %>%
unlist %>%
order()
colnames(dat)[ord]
New approach
The new data not only has to numerical values but also a string value (east, north, south, west
). I realised that I could use the same solution if I give east
a value of 1
, north
of 2
and so on. The sum of the three numbers than still provides the correct order.
I have been having some trouble adapting the code though.
ord <- gsub("\\[|\\]|\\)", "", colnames(new_dat)) %>%
# provides "0,25 east", "0,25 north" etc
strsplit(",") %>%
# provides "0" and "25 east", "0" and "25 north" etc
lapply(as.numeric) %>%
lapply(sum) %>%
# SHOULD provide 0+25+1 (east), 0+25+2 (north) etc
unlist %>%
order()
The issue lies in splitting the string in 3 parts, and convert the directions to a number, IF, and ONLY IF, there are three parts. Otherwise it should just use the two. How should I do this?