I have a data frame that includes country/year import and export, towards other countries. As in the example dataset, data on dyadic import and export do not overlap perfectly.
e.g.
library(tidyverse)
df <- data.frame("Reporter" = c("USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "Africa","Africa", "Africa","Africa", "Africa","Africa", "Africa","Africa", "EU", "EU","EU", "EU", "EU", "EU","EU", "EU"),
"Partner" = c("Africa","Africa", "Africa","Africa","EU", "EU","EU", "EU", "USA", "USA", "USA", "USA", "EU", "EU","EU", "EU","USA", "USA", "USA", "USA","Africa","Africa", "Africa","Africa"),
"Year" = c(1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980, 1970, 1970, 1980, 1980),
"Flow" = c("Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export","Import", "Export"),
"Val" = runif(24, min=0, max=100), stringsAsFactors = FALSE)
# Reporter Partner Year Flow Val
# 1 USA Africa 1970 Import 13.169790
# 2 USA Africa 1970 Export 28.531263
# 3 USA Africa 1980 Import 66.811160
# 4 USA Africa 1980 Export 47.556102
# 5 USA EU 1970 Import 59.166556
# 6 USA EU 1970 Export 71.032895
# 7 USA EU 1980 Import 89.688642
# 8 USA EU 1980 Export 36.563593
# 9 Africa USA 1970 Import 33.088294
# 10 Africa USA 1970 Export 10.692528
# 11 Africa USA 1980 Import 69.296384
# 12 Africa USA 1980 Export 54.697131
# 13 Africa EU 1970 Import 64.327314
# 14 Africa EU 1970 Export 64.659566
# 15 Africa EU 1980 Import 6.139465
# 16 Africa EU 1980 Export 97.317815
# 17 EU USA 1970 Import 7.245794
# 18 EU USA 1970 Export 72.291265
# 19 EU USA 1980 Import 14.134386
# 20 EU USA 1980 Export 60.288242
# 21 EU Africa 1970 Import 29.648374
# 22 EU Africa 1970 Export 81.916536
# 23 EU Africa 1980 Import 47.665834
# 24 EU Africa 1980 Export 64.307639
and I create the wide version of this data.
wide_df <- df %>% spread ("Flow", "Val")
I am able to create directional IDs for dyads.
wide_df$ReporterID <- as.numeric(factor(wide_df$Reporter, levels=unique(wide_df$Reporter)))
However, the resulting data consider as different, for example, the dyads USA, and Africa, and Africa and USA.
Question: How can I create a unique ID for each dyad?
Can anyone think of a way that allows me to collapse these dyads into a single ID code