i have a data set like that :
# install.packages(dplyr)
library(dplyr)
df <- tibble(period = c(201501,201502,201503,201504,201505,201506,201507,201508,201509,201510,201511,201512,201513),
sales = sample(1:100,13),
P1 = c(1,0,0,0,0,0,0,0,0,0,0,0,0),
P10 = c(0,0,0,0,0,0,0,0,0,1,0,0,0),
P11 = c(0,0,0,0,0,0,0,0,0,0,1,0,0),
P12 = c(0,0,0,0,0,0,0,0,0,0,0,1,0),
P13 = c(0,0,0,0,0,0,0,0,0,0,0,0,1),
P2 = c(0,1,0,0,0,0,0,0,0,0,0,0,0),
P3 = c(0,0,1,0,0,0,0,0,0,0,0,0,0),
P4 = c(0,0,0,1,0,0,0,0,0,0,0,0,0),
P5 = c(0,0,0,0,1,0,0,0,0,0,0,0,0),
P6 = c(0,0,0,0,0,1,0,0,0,0,0,0,0),
P7 = c(0,0,0,0,0,0,1,0,0,0,0,0,0),
P8 = c(0,0,0,0,0,0,0,1,0,0,0,0,0),
P9 = c(0,0,0,0,0,0,0,0,1,0,0,0,0),
)
print(df)
so i have this :
# A tibble: 13 x 15
period sales P1 P10 P11 P12 P13 P2 P3 P4 P5 P6
<dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 201501 92 1 0 0 0 0 0 0 0 0 0
2 201502 60 0 0 0 0 0 1 0 0 0 0
3 201503 31 0 0 0 0 0 0 1 0 0 0
4 201504 74 0 0 0 0 0 0 0 1 0 0
5 201505 82 0 0 0 0 0 0 0 0 1 0
6 201506 86 0 0 0 0 0 0 0 0 0 1
7 201507 19 0 0 0 0 0 0 0 0 0 0
8 201508 32 0 0 0 0 0 0 0 0 0 0
9 201509 99 0 0 0 0 0 0 0 0 0 0
10 201510 47 0 1 0 0 0 0 0 0 0 0
11 201511 21 0 0 1 0 0 0 0 0 0 0
12 201512 77 0 0 0 1 0 0 0 0 0 0
13 201513 25 0 0 0 0 1 0 0 0 0 0
# ... with 3 more variables: P7 <dbl>, P8 <dbl>, P9 <dbl>
is there an automated way to get the same tibble but with the P+number columns in the right order : P1,P2,P3,P4 etc etc...