How can I automatically (or systematically) rearrange the columns of my dataframe according to some elements contained in the name of the columns. For example,
df <- data.frame(name_001_a=letters[1:4],
value_003_a=c(rep(TRUE, 2), rep(FALSE, 2)),
other_002_a=letters[5:8])
I like to rearrange the columns according to the number in the name of the columns (001, 002, 003), so my dataframe is:
> df <- df[c(1,3,2)]
name_001_a other_002_a value_003_a
1 a e TRUE
2 b f TRUE
3 c g FALSE
4 d h FALSE
How can I effectively do that with a large number of variables? Thxs