I've got a dataframe pulled from an external source with column labels based on year and week counts. Unfortunately, the columns pull in a weird non-sequential order (feature of the external dataset), and so when I report on them I'm going to want to pull the columns using "select" to get them in date-sequential order.
I want to insert a zero before the single-digit column labels below -- that is, "W1_2019" becomes "W01_2019" (and so forth for 2, 3, and up to 9), but not before the double-digit ones -- that is ""W10_2019" will remain as-is. The resulting column should allow me to order names(df) in ascending order, with W01 followed by W02 and W03. Without the zeros, of course, the order is W1 followed by W10 and then W2 which is exactly what I don't want.
See code below.
df<-setNames(
data.frame(
t(data.frame(c("1","2","1","3","2","3", "1")))
,row.names = NULL,stringsAsFactors = FALSE
),
c("W10_2018", "W50_2018", "W51_2018", "W52_2018", "W1_2019", "W2_2019", "W3_2019")
)
names(df) = gsub(pattern="W#_.*", replacement = "W0#_", x=names(df))
sort(names(df))
The gsub line doesn't return an error, but it also doesn't change the names. The result is that the output of the "sort" line is:
[1] "W1_2019" "W10_2018" "W2_2019" "W3_2019" "W50_2018" "W51_2018" "W52_2018"
What it should look like if successful is:
[1] "W01_2019" "W02_2019" "W03_2019" "W10_2018" "W50_2018" "W51_2018" "W52_2018"