在具有许多列的数据集中传播单个 variable/column,聚合其他变量

Spreading a single variable/column in a dataset with many columns, aggregating the other variables

我有一个数据集如下(有关更广泛的示例,请参阅下面的编辑):

DT <- structure(list(Geo_level = c(50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L
), State_Code = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), County_Code = c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), State = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Alabama", class = "factor"), 
    County = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Autauga County", class = "factor"), 
    year = c(1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
    1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
    1995L, 1995L, 1995L, 1995L), Age_Group = c(0L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 18L, 99L), Population = c(576L, 2445L, 3622L, 3744L, 
    3266L, 2344L, 2710L, 3033L, 4002L, 3678L, 3028L, 2610L, 2301L, 
    1904L, 1549L, 1153L, 835L, 516L, 435L, 43751L), Male_Population = c(263L, 
    1237L, 1875L, 1959L, 1679L, 1182L, 1277L, 1471L, 1941L, 1784L, 
    1501L, 1241L, 1085L, 931L, 711L, 498L, 330L, 172L, 115L, 
    21252L), Female_Population = c(313L, 1208L, 1747L, 1785L, 
    1587L, 1162L, 1433L, 1562L, 2061L, 1894L, 1527L, 1369L, 1216L, 
    973L, 838L, 655L, 505L, 344L, 320L, 22499L), WA_MALE = c(215L, 
    971L, 1494L, 1553L, 1279L, 915L, 1044L, 1252L, 1652L, 1484L, 
    1244L, 1062L, 934L, 808L, 620L, 426L, 271L, 146L, 86L, 17456L
    ), WA_FEMALE = c(248L, 954L, 1389L, 1382L, 1195L, 869L, 1120L, 
    1265L, 1706L, 1505L, 1224L, 1130L, 1034L, 838L, 693L, 535L, 
    408L, 271L, 253L, 18019L)), class = c("data.table", "data.frame"
), row.names = c(NA, -20L))

    Geo_level State_Code County_Code   State         County year Age_Group Population Male_Population Female_Population WA_MALE WA_FEMALE
 1:        50          1           1 Alabama Autauga County 1995         0        576             263               313     215       248
 2:        50          1           1 Alabama Autauga County 1995         1       2445            1237              1208     971       954
 3:        50          1           1 Alabama Autauga County 1995         2       3622            1875              1747    1494      1389
 4:        50          1           1 Alabama Autauga County 1995         3       3744            1959              1785    1553      1382
 5:        50          1           1 Alabama Autauga County 1995         4       3266            1679              1587    1279      1195
 6:        50          1           1 Alabama Autauga County 1995         5       2344            1182              1162     915       869
 7:        50          1           1 Alabama Autauga County 1995         6       2710            1277              1433    1044      1120
 8:        50          1           1 Alabama Autauga County 1995         7       3033            1471              1562    1252      1265
 9:        50          1           1 Alabama Autauga County 1995         8       4002            1941              2061    1652      1706
10:        50          1           1 Alabama Autauga County 1995         9       3678            1784              1894    1484      1505
11:        50          1           1 Alabama Autauga County 1995        10       3028            1501              1527    1244      1224
12:        50          1           1 Alabama Autauga County 1995        11       2610            1241              1369    1062      1130
13:        50          1           1 Alabama Autauga County 1995        12       2301            1085              1216     934      1034
14:        50          1           1 Alabama Autauga County 1995        13       1904             931               973     808       838
15:        50          1           1 Alabama Autauga County 1995        14       1549             711               838     620       693
16:        50          1           1 Alabama Autauga County 1995        15       1153             498               655     426       535
17:        50          1           1 Alabama Autauga County 1995        16        835             330               505     271       408
18:        50          1           1 Alabama Autauga County 1995        17        516             172               344     146       271
19:        50          1           1 Alabama Autauga County 1995        18        435             115               320      86       253
20:        50          1           1 Alabama Autauga County 1995        99      43751           21252             22499   17456     18019

我正在尝试用 dcast 传播 Age_Group 变量,但我不知道该怎么做。我一直遇到的问题之一是如何告诉 dcast 仅需要更改的列,而不是单独列出所有列。我也从来没有真正理解我的 value.var 应该是什么。

有没有人可以向我解释如何处理这个问题?

编辑:期望的结果是:

DT_desired_result <- structure(list(Geo_level = 50L, State_Code = 1L, County_Code = 1L, 
    State = structure(1L, .Label = "Alabama", class = "factor"), 
    County = structure(1L, .Label = "Autauga County", class = "factor"), 
    year = 1995L, Age_Group = 99L, Population = 43751L, Male_Population = 21252L, 
    Female_Population = 22499L, WA_MALE = 17456L, WA_FEMALE = 18019L, 
    V1 = 576L, V2 = 2445L, V3 = 3622L, V4 = 3744L, V5 = 3266L, 
    V6 = 2344L, V7 = 2710L, V8 = 3033L, V9 = 4002L, V10 = 3678L, 
    V11 = 3028L, V12 = 2610L, V13 = 2301L, V14 = 1904L, V15 = 1549L, 
    V16 = 1153L, V17 = 835L, V18 = 516L, V19 = 435L, V20 = 43751L), row.names = c(NA, 
-1L), class = c("data.table", "data.frame")

其中V变量对应PopulationAge_Group.

编辑 II,代码基于以下答案:

执行结果:

 test <- DT %>% 
     group_by(State, year, County)  %>% 
     pivot_wider(names_from = Age_Group, values_from = Population) %>% 
     fill(`0`:`99`) %>% 
     head(21)

基于实际数据

编辑 III:新 DPUT(见评论回答)

DTtwo <- structure(list(Geo_level = c(50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L), State_Code = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 56L, 56L, 56L, 56L, 56L, 
56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 
56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 
56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 
56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L), County_Code = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 45L, 
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 45L, 45L, 45L, 45L, 45L, 45L, 
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 
45L), State = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Alabama", 
"Wyoming"), class = "factor"), County = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("Autauga County", "Barbour County", "Weston County"
), class = "factor"), year = c(1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L), Age_Group = c(0L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
99L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 99L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 99L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 99L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 99L, 0L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 99L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 99L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 99L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 99L), Population = c(576L, 2445L, 
3622L, 3744L, 3266L, 2344L, 2710L, 3033L, 4002L, 3678L, 3028L, 
2610L, 2301L, 1904L, 1549L, 1153L, 835L, 516L, 435L, 43751L, 
587L, 2604L, 3596L, 4016L, 3408L, 2505L, 2567L, 3165L, 3892L, 
4072L, 3264L, 2823L, 2526L, 2067L, 1685L, 1253L, 890L, 546L, 
443L, 45909L, 354L, 1434L, 2056L, 2156L, 2148L, 1929L, 2058L, 
2008L, 2350L, 2185L, 1956L, 1970L, 1423L, 1141L, 1061L, 978L, 
806L, 517L, 512L, 29042L, 364L, 1423L, 1891L, 2074L, 2062L, 1988L, 
1873L, 2053L, 2207L, 2131L, 2045L, 1991L, 1632L, 1159L, 1034L, 
978L, 753L, 535L, 460L, 28653L, 57L, 289L, 355L, 511L, 579L, 
296L, 314L, 343L, 493L, 597L, 575L, 484L, 368L, 345L, 274L, 254L, 
218L, 157L, 134L, 6643L, 62L, 288L, 355L, 442L, 553L, 350L, 280L, 
355L, 385L, 577L, 595L, 514L, 429L, 337L, 302L, 236L, 239L, 143L, 
136L, 6578L, 576L, 2445L, 3622L, 3744L, 3266L, 2344L, 2710L, 
3033L, 4002L, 3678L, 3028L, 2610L, 2301L, 1904L, 1549L, 1153L, 
835L, 516L, 435L, 43751L, 354L, 1434L, 2056L, 2156L, 2148L, 1929L, 
2058L, 2008L, 2350L, 2185L, 1956L, 1970L, 1423L, 1141L, 1061L, 
978L, 806L, 517L, 512L, 29042L, 57L, 289L, 355L, 511L, 579L, 
296L, 314L, 343L, 493L, 597L, 575L, 484L, 368L, 345L, 274L, 254L, 
218L, 157L, 134L, 6643L), Male_Population = c(263L, 1237L, 1875L, 
1959L, 1679L, 1182L, 1277L, 1471L, 1941L, 1784L, 1501L, 1241L, 
1085L, 931L, 711L, 498L, 330L, 172L, 115L, 21252L, 269L, 1237L, 
1844L, 2135L, 1784L, 1241L, 1232L, 1536L, 1895L, 2011L, 1602L, 
1388L, 1196L, 1025L, 784L, 530L, 357L, 175L, 121L, 22362L, 185L, 
745L, 1075L, 1081L, 1117L, 1184L, 1229L, 1156L, 1309L, 1192L, 
986L, 1002L, 696L, 534L, 493L, 400L, 288L, 179L, 123L, 14974L, 
187L, 732L, 987L, 1028L, 1072L, 1200L, 1166L, 1213L, 1235L, 1149L, 
1077L, 1031L, 801L, 551L, 484L, 407L, 306L, 174L, 120L, 14920L, 
25L, 154L, 178L, 246L, 305L, 184L, 163L, 172L, 252L, 309L, 320L, 
266L, 168L, 169L, 132L, 132L, 98L, 64L, 35L, 3372L, 29L, 144L, 
184L, 221L, 293L, 208L, 144L, 200L, 191L, 305L, 319L, 288L, 212L, 
156L, 152L, 108L, 110L, 60L, 40L, 3364L, 263L, 1237L, 1875L, 
1959L, 1679L, 1182L, 1277L, 1471L, 1941L, 1784L, 1501L, 1241L, 
1085L, 931L, 711L, 498L, 330L, 172L, 115L, 21252L, 185L, 745L, 
1075L, 1081L, 1117L, 1184L, 1229L, 1156L, 1309L, 1192L, 986L, 
1002L, 696L, 534L, 493L, 400L, 288L, 179L, 123L, 14974L, 25L, 
154L, 178L, 246L, 305L, 184L, 163L, 172L, 252L, 309L, 320L, 266L, 
168L, 169L, 132L, 132L, 98L, 64L, 35L, 3372L), Female_Population = c(313L, 
1208L, 1747L, 1785L, 1587L, 1162L, 1433L, 1562L, 2061L, 1894L, 
1527L, 1369L, 1216L, 973L, 838L, 655L, 505L, 344L, 320L, 22499L, 
318L, 1367L, 1752L, 1881L, 1624L, 1264L, 1335L, 1629L, 1997L, 
2061L, 1662L, 1435L, 1330L, 1042L, 901L, 723L, 533L, 371L, 322L, 
23547L, 169L, 689L, 981L, 1075L, 1031L, 745L, 829L, 852L, 1041L, 
993L, 970L, 968L, 727L, 607L, 568L, 578L, 518L, 338L, 389L, 14068L, 
177L, 691L, 904L, 1046L, 990L, 788L, 707L, 840L, 972L, 982L, 
968L, 960L, 831L, 608L, 550L, 571L, 447L, 361L, 340L, 13733L, 
32L, 135L, 177L, 265L, 274L, 112L, 151L, 171L, 241L, 288L, 255L, 
218L, 200L, 176L, 142L, 122L, 120L, 93L, 99L, 3271L, 33L, 144L, 
171L, 221L, 260L, 142L, 136L, 155L, 194L, 272L, 276L, 226L, 217L, 
181L, 150L, 128L, 129L, 83L, 96L, 3214L, 313L, 1208L, 1747L, 
1785L, 1587L, 1162L, 1433L, 1562L, 2061L, 1894L, 1527L, 1369L, 
1216L, 973L, 838L, 655L, 505L, 344L, 320L, 22499L, 169L, 689L, 
981L, 1075L, 1031L, 745L, 829L, 852L, 1041L, 993L, 970L, 968L, 
727L, 607L, 568L, 578L, 518L, 338L, 389L, 14068L, 32L, 135L, 
177L, 265L, 274L, 112L, 151L, 171L, 241L, 288L, 255L, 218L, 200L, 
176L, 142L, 122L, 120L, 93L, 99L, 3271L), WA_MALE = c(215L, 971L, 
1494L, 1553L, 1279L, 915L, 1044L, 1252L, 1652L, 1484L, 1244L, 
1062L, 934L, 808L, 620L, 426L, 271L, 146L, 86L, 17456L, 207L, 
945L, 1456L, 1674L, 1351L, 957L, 1003L, 1285L, 1608L, 1674L, 
1314L, 1165L, 1034L, 883L, 681L, 465L, 309L, 143L, 91L, 18245L, 
85L, 335L, 472L, 455L, 481L, 539L, 614L, 596L, 684L, 650L, 545L, 
615L, 472L, 384L, 353L, 290L, 198L, 104L, 60L, 7932L, 98L, 325L, 
407L, 439L, 470L, 508L, 569L, 611L, 623L, 606L, 595L, 604L, 531L, 
375L, 360L, 288L, 215L, 107L, 68L, 7799L, 24L, 146L, 172L, 236L, 
292L, 175L, 161L, 163L, 245L, 304L, 314L, 263L, 163L, 168L, 129L, 
132L, 96L, 63L, 35L, 3281L, 28L, 136L, 176L, 210L, 282L, 198L, 
142L, 189L, 184L, 299L, 313L, 285L, 207L, 155L, 149L, 108L, 109L, 
59L, 40L, 3269L, 215L, 971L, 1494L, 1553L, 1279L, 915L, 1044L, 
1252L, 1652L, 1484L, 1244L, 1062L, 934L, 808L, 620L, 426L, 271L, 
146L, 86L, 17456L, 85L, 335L, 472L, 455L, 481L, 539L, 614L, 596L, 
684L, 650L, 545L, 615L, 472L, 384L, 353L, 290L, 198L, 104L, 60L, 
7932L, 24L, 146L, 172L, 236L, 292L, 175L, 161L, 163L, 245L, 304L, 
314L, 263L, 163L, 168L, 129L, 132L, 96L, 63L, 35L, 3281L), WA_FEMALE = c(248L, 
954L, 1389L, 1382L, 1195L, 869L, 1120L, 1265L, 1706L, 1505L, 
1224L, 1130L, 1034L, 838L, 693L, 535L, 408L, 271L, 253L, 18019L, 
262L, 1059L, 1346L, 1493L, 1218L, 971L, 1001L, 1313L, 1648L, 
1655L, 1323L, 1163L, 1121L, 906L, 757L, 606L, 425L, 293L, 264L, 
18824L, 77L, 270L, 397L, 469L, 452L, 327L, 400L, 418L, 518L, 
522L, 509L, 555L, 460L, 401L, 360L, 325L, 345L, 215L, 219L, 7239L, 
82L, 291L, 364L, 431L, 417L, 345L, 347L, 416L, 464L, 495L, 513L, 
524L, 507L, 397L, 348L, 349L, 265L, 233L, 207L, 6995L, 31L, 127L, 
170L, 257L, 264L, 105L, 145L, 167L, 232L, 281L, 252L, 211L, 194L, 
172L, 138L, 122L, 120L, 93L, 99L, 3180L, 32L, 136L, 164L, 211L, 
251L, 134L, 130L, 151L, 185L, 265L, 273L, 219L, 211L, 178L, 147L, 
128L, 128L, 83L, 96L, 3122L, 248L, 954L, 1389L, 1382L, 1195L, 
869L, 1120L, 1265L, 1706L, 1505L, 1224L, 1130L, 1034L, 838L, 
693L, 535L, 408L, 271L, 253L, 18019L, 77L, 270L, 397L, 469L, 
452L, 327L, 400L, 418L, 518L, 522L, 509L, 555L, 460L, 401L, 360L, 
325L, 345L, 215L, 219L, 7239L, 31L, 127L, 170L, 257L, 264L, 105L, 
145L, 167L, 232L, 281L, 252L, 211L, 194L, 172L, 138L, 122L, 120L, 
93L, 99L, 3180L)), row.names = c(NA, -180L), class = c("data.table", 
"data.frame"))
DT %>% group_by(County, State, year) %>% slice(1:4) %>% head(16)

# A tibble: 16 x 55
# Groups:   County, State, year [4]
   Geo_level State_Code County_Code State County  year Age_Group Population Male_Population Female_Populati~ WA_MALE WA_FEMALE BA_MALE BA_FEMALE IA_MALE IA_FEMALE AA_MALE
       <int>      <int>       <int> <fct> <fct>  <int>     <int>      <int>           <int>            <int>   <int>     <int>   <int>     <int>   <int>     <int>   <int>
 1        50          1           1 Alab~ Autau~  1995         0        576             263              313     215       248      43        60       0         2       3
 2        50          1           1 Alab~ Autau~  1995         1       2445            1237             1208     971       954     245       227       2         4       3
 3        50          1           1 Alab~ Autau~  1995         2       3622            1875             1747    1494      1389     352       327       9         6       4
 4        50          1           1 Alab~ Autau~  1995         3       3744            1959             1785    1553      1382     375       364       9        11       1
 5        50          1           1 Alab~ Autau~  2000         0        586             267              319     217       252      44        60       0         2       3
 6        50          1           1 Alab~ Autau~  2000         1       2443            1236             1207     969       955     243       226       2         4       3
 7        50          1           1 Alab~ Autau~  2000         2       3619            1874             1745    1494      1389     351       326       9         6       4
 8        50          1           1 Alab~ Autau~  2000         3       3776            1976             1800    1565      1393     379       367       9        11       1
 9        50          1           1 Alab~ Autau~  2001         0        621             303              318     244       251      51        58       0         2       2
10        50          1           1 Alab~ Autau~  2001         1       2499            1223             1276     956       986     240       256       2         4       4
11        50          1           1 Alab~ Autau~  2001         2       3541            1807             1734    1425      1347     349       352       9         6       6
12        50          1           1 Alab~ Autau~  2001         3       3896            2028             1868    1598      1468     390       360      12        11       6
13        50          1           1 Alab~ Autau~  2002         0        587             269              318     207       262      55        49       0         1       1
14        50          1           1 Alab~ Autau~  2002         1       2604            1237             1367     945      1059     259       262       2         4       5
15        50          1           1 Alab~ Autau~  2002         2       3596            1844             1752    1456      1346     353       364       9         6       7
16        50          1           1 Alab~ Autau~  2002         3       4016            2135             1881    1674      1493     417       346      10        11       8
# ... with 38 more variables: AA_FEMALE <int>, NA_MALE <int>, NA_FEMALE <int>, TOM_MALE <int>, TOM_FEMALE <int>, NH_MALE <int>, NH_FEMALE <int>, NHWA_MALE <int>,
#   NHWA_FEMALE <int>, NHBA_MALE <int>, NHBA_FEMALE <int>, NHIA_MALE <int>, NHIA_FEMALE <int>, NHAA_MALE <int>, NHAA_FEMALE <int>, NHNA_MALE <int>, NHNA_FEMALE <int>,
#   NHTOM_MALE <int>, NHTOM_FEMALE <int>, H_MALE <int>, H_FEMALE <int>, HWA_MALE <int>, HWA_FEMALE <int>, HBA_MALE <int>, HBA_FEMALE <int>, HIA_MALE <int>,
#   HIA_FEMALE <int>, HAA_MALE <int>, HAA_FEMALE <int>, HNA_MALE <int>, HNA_FEMALE <int>, HTOM_MALE <int>, HTOM_FEMALE <int>, Female_perc <dbl>, Male_perc <dbl>,
#   orig_year <int>, orig_age_grp <int>, orig_pop <int>

你可以试试

DTtwo %>% 
 select(State, County, year, Age_Group, Population) %>% 
 pivot_wider(names_from = Age_Group, values_from = Population, values_fn = unique) %>%   
 rename(Population = `99`)
# A tibble: 6 x 23
  State  County     year   `0`   `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`  `10`  `11`  `12`  `13`  `14`  `15`  `16`  `17`  `18` Population
  <fct>  <fct>     <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>      <int>
1 Alaba~ Autauga ~  1995   576  2445  3622  3744  3266  2344  2710  3033  4002  3678  3028  2610  2301  1904  1549  1153   835   516   435      43751
2 Alaba~ Autauga ~  2002   587  2604  3596  4016  3408  2505  2567  3165  3892  4072  3264  2823  2526  2067  1685  1253   890   546   443      45909
3 Alaba~ Barbour ~  1995   354  1434  2056  2156  2148  1929  2058  2008  2350  2185  1956  1970  1423  1141  1061   978   806   517   512      29042
4 Alaba~ Barbour ~  2002   364  1423  1891  2074  2062  1988  1873  2053  2207  2131  2045  1991  1632  1159  1034   978   753   535   460      28653
5 Wyomi~ Weston C~  1995    57   289   355   511   579   296   314   343   493   597   575   484   368   345   274   254   218   157   134       6643
6 Wyomi~ Weston C~  2002    62   288   355   442   553   350   280   355   385   577   595   514   429   337   302   236   239   143   136       6578

您可以使用 left_joinby = c(State, County, year)

添加此输出