R dplyr收集从宽到长的多列多个值

我有以下多种形式的数据:

identity = c("Race1", "Race2", "Race3")
total_2017 = c(300,325,350)
total_2018 = c(200,225,250)
total_2019 = c(100,150,200)
pct_2017 = total_2017/sum(total_2017[1],total_2018[1],total_2019[1])
pct_2018 = total_2018/sum(total_2017[2],total_2018[2],total_2019[2])
pct_2019 = total_2019/sum(total_2017[3],total_2018[3],total_2019[3])
df.wide <- cbind.data.frame(identity, total_2017, total_2018, total_2019, pct_2017, pct_2018, pct_2019)

宽数据如下所示:

     identity total_2017 total_2018 total_2019  pct_2017  pct_2018 pct_2019
1    Race1        300        200        100 0.5000000 0.2857143   0.1250
2    Race2        325        225        150 0.5416667 0.3214286   0.1875
3    Race3        350        250        200 0.5833333 0.3571429   0.2500

The 3rd, 4th and 5th columns are the totals of "identity" for years 2017 to 2019, and the last three columns are the respective shares. I want to convert it into long format such that the totals are gathered into a column Enrollment and the percentages are gathered into a column Percent. I try the following code:

    library(dplyr)
    library(magrittr)
    library(tidyr)

df.long <- df.wide %>% 
  gather(key = "Total", value = "Enrollment", starts_with("total_")) %>%
  gather(key = "Share", value = "Percent", starts_with("pct_"))

这是长格式数据的前10行。

    head(df.long, 10)
   identity      Total Enrollment    Share   Percent
1     Race1 total_2017        300 pct_2017 0.5000000
2     Race2 total_2017        325 pct_2017 0.5416667
3     Race3 total_2017        350 pct_2017 0.5833333
4     Race1 total_2018        200 pct_2017 0.5000000
5     Race2 total_2018        225 pct_2017 0.5416667
6     Race3 total_2018        250 pct_2017 0.5833333
7     Race1 total_2019        100 pct_2017 0.5000000
8     Race2 total_2019        150 pct_2017 0.5416667
9     Race3 total_2019        200 pct_2017 0.5833333
10    Race1 total_2017        300 pct_2018 0.2857143

可以看出,注册和百分比的顺序不同。列中的顺序如何?