Reputation: 1304
I have a trade dataset similar to the following:
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("Asia", "Asia", "Asia", "Asia","Africa","Africa", "Africa","Africa","EU", "EU","EU", "EU", "USA", "USA", "USA", "USA", "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)
To note that while all the reporter countries are also partner setdiff(df$Reporter, df$Partner)
. The contrary is not true setdiff(df$Partner, df$Reporter)
. In the real data I have 61 more partner countries than reporters.
I can set unique IDs for both my reporter and partner variables
df$repID <- as.numeric(factor(df$Reporter,
levels=unique(df$Reporter)))
df$partID <- as.numeric(factor(df$Partner,
levels=unique(df$Partner)))
The problem of using this technique is that IDs do not match across the two partID and repID. e.g. while USA ID is "1" in repID, in partID is "4".
I would like to create country IDs that are consistent across the two columns, so for example that USA is "1" in both repID and partID. Also, it is important that all the Partners that are not reporters (i.e. Asia) have their own and unique code.
Upvotes: 1
Views: 252
Reputation: 2782
Both factors should have the same levels. You should use the larger set (Partner) for the levels for both factors.
df$repID <- as.numeric(factor(df$Reporter, levels=unique(df$Partner)))
df$partID <- as.numeric(factor(df$Partner, levels=unique(df$Partner)))
Upvotes: 1