简体   繁体   中英

Merging dyadic and monadic datasets in R

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

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.

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

The second monadic dataset has the country, and GDP.

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

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

Thanks in advance =)

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)

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM