【问题标题】:Creating new variables using different datasets使用不同的数据集创建新变量
【发布时间】: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 函数。但我想不通。

你知道如何解决这个问题吗?

【问题讨论】:

  • 你能展示你的加入尝试吗?听起来你在正确的轨道上

标签: r join dplyr merge


【解决方案1】:

如果我正确理解您的问题,您唯一需要做的就是dplyr::left_joindf1 是国家数据集,df1 是母亲数据集。

left_join(df2, df1)
## Joining, by = c("country", "age_class")
##        caseid   weight country age age_class [...] nb_singleton nb_twin  prob_twin nb_under_5_dead nb_under_5_dead_singleton
## 1 00010001 02 1.086089     AO7  38     35-39 [...]         4784     150 0.03040130             545                       509
## 2 00010002 02 1.086089     AO7  40     40-44 [...]         4647     193 0.03987603             726                       660
## 3 00010002 03 1.086089     AO7  16     15-19 [...]           28       0 0.00000000               2                         2
## 4 00010003 02 1.086089     AO7  43     40-44 [...]         4647     193 0.03987603             726                       660
## 5 00010004 02 1.086089     AO7  25     25-29 [...]         3256      82 0.02456561             327                       302
## 6 00010006 01 1.086089     AO7  26     25-29 [...]         3256      82 0.02456561             327                       302

dplyr::left_join 保留第一个 data.frame 中的所有行,并添加第二个 data.frame 中的匹配行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多