【发布时间】:2021-06-08 08:40:19
【问题描述】:
我正在尝试合并两个具有不同结构的数据集。第一个数据集包含国家级别、每个国家和每个年龄段的指标,如下所示:
country age_class nb_birth nb_singleton nb_twin prob_twin nb_under_5_dead nb_under_5_dead_singleton
1 AO7 15-19 28 28 0 0.000000000 2 2
2 AO7 20-24 1133 1123 10 0.008826125 107 101
3 AO7 25-29 3338 3256 82 0.024565608 327 302
4 AO7 30-34 4152 4059 93 0.022398844 425 402
5 AO7 35-39 4934 4784 150 0.030401297 545 509
6 AO7 40-44 4840 4647 193 0.039876033 726 660
第二个数据集包含个体层面的指标,每个母亲,如下所示:
caseid weight country age age_class region region_type education wealth parity
1 00010001 02 1.086089 AO7 38 35-39 benguela rural no education poorest 7
2 00010002 02 1.086089 AO7 40 40-44 benguela rural no education poorest 6
3 00010002 03 1.086089 AO7 16 15-19 benguela rural no education poorest 1
4 00010003 02 1.086089 AO7 43 40-44 benguela rural primary poorest 8
5 00010004 02 1.086089 AO7 25 25-29 benguela rural no education poorest 6
6 00010006 01 1.086089 AO7 26 25-29 benguela rural no education poorest 4
如您所见,变量“国家”和“年龄等级”是共享的。我要做的是将国家数据集中的指标分配给母数据集中的每一行。我想得到这样的结果:
caseid weight country age age_class [...] nb_births nb_singleton nb_twin prob_twin
1 00010001 02 1.086089 AO7 38 35-39 [...] 4934 4784 150 0.0304
2 00010002 02 1.086089 AO7 40 40-44 [...] 4840 4647 193 0.0399
3 00010002 03 1.086089 AO7 16 15-19 [...] 28 28 0 0.0000
最终,来自同一国家和同一年龄段的所有母亲将具有相同的国家数据集中变量值。
我正在使用 dplyr 包并使用过 join 函数和 mutate 函数。但我想不通。
你知道如何解决这个问题吗?
【问题讨论】:
-
你能展示你的加入尝试吗?听起来你在正确的轨道上