在 R 中合并二元和一元数据集 [英] Merging dyadic and monadic datasets in R

查看:19
本文介绍了在 R 中合并二元和一元数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在 R 中合并两个数据集(使用 Rstudio).

I am currently attempting to merge two datasets in R (using Rstudio).

第一个数据集是由战争相关数据集中的国家之间的双边贸易流 (dyad) 组成的二元数据集.第二个是来自 Penn World Tables 的每个国家的 GDP 的一元数据.我想知道如何合并数据集,以便每个对子都有国家 1 的 GDP 和国家 2 的 GDP.

The first dataset is dyadic consisting of bilateral trade flows between countries (dyad) from the Correlates of War dataset. The second is monadic data of GDP for each country from the Penn World Tables. I would like to know how to go about merging the datasets so that each dyad has the GDP of country 1 and GDP of country 2.

两个数据集都根据 3 个字符 isocodes 进行编码.第一个二元数据集有以下列:Country1, country2, flow1, flow2, distance

Both data sets are coded according to the 3 character isocodes. The first dyadic dataset has the following coloumns: Country1, country2, flow1, flow2, distance

第二个单子数据集包含国家和 GDP.

The second monadic dataset has the country, and GDP.

我想添加 GDP 数据,这样新的数据集现在将是:Country1,country2,flow1,flow2,距离,gdp1,gdp2.

I would like to add the GDP data so that the new data set will now be: Country1, country2, flow1, flow2, distance, gdp1, gdp2.

有谁知道我如何在 R 中合并这些集合?

Does anyone know how i can merge these sets in R?

提前致谢 =)

推荐答案

如果你想要一个具体的答案,你应该发布一个可复制的数据样本.这应该适用于有向和无向二元组.假设您的二元 df 的国家代码是 ccode1ccode2 并且您的 dfs 被命名为 dy.dfmon.df分别.

You should post a replicable sample of your data if you want a specific answer. This should work for both directed and undirected dyads. Assuming your country codes for the dyadic df are ccode1 and ccode2 and your dfs are named dy.df and mon.df respectively.

new.df <- merge(dy.df, mon.df, by = c('ccode1', 'year'), all.x = TRUE)
new.df <- merge(dy.df, mon.df, by = c('ccode2', 'year'), all.x = TRUE)

然后您可以从那里清除您的数据集.你也确定你有ISO代码吗?大多数 COW 的东西使用 COW 代码,而不是 ISO 代码.我还建议您阅读有关 merge 命令的文档.帮助(合并).

Then you can clean out your data set from there. Also are you sure you have ISO codes? Most of the COW stuff uses COW codes, not ISO codes. I'd also suggest your read the documentation on the merge command. help(merge).

这篇关于在 R 中合并二元和一元数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆