Search code examples
rdata-manipulationpairwise

Create dyadic pairwise dataset based on conditions


I have a monadic dataset that looks something like this:

df<-structure(list(Number = c("375_1", "375_1", "375_1", "375_1", 
"375_1", "375_1", "375_1", "375_1", "647_1", "647_1", "647_1", 
"647_1", "647_1", "647_1", "647_1", "647_1", "647_1", "647_1", 
"647_1", "647_1"), year = c(1973, 1973, 1973, 1973, 1973, 1973, 
1973, 1973, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 
1981, 1981, 1981), Country = c("AUT", "PRT", "CHE", "NOR", "SWE", 
"ISL", "DNK", "GBR", "BRA", "CHL", "EGY", "IND", "ISR", "MEX", 
"PER", "KOR", "PAK", "PHL", "TUN", "TUR")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

I would like to transform this dataset to have a dyadic structure based on the Number column. In other words, for every different Number, I would like to create pairs of observations for all the combinations of countries. The "head" of the final dataset should look something like this:

final <- data.frame(Number = c("375_1", "375_1", "375_1", "375_1", "375_1", "375_1", "375_1","375_1", "375_1", "375_1", "375_1","375_1"), 
                    year = c(1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973,1973, 1973, 1973, 1973), 
                    Country1 = c("AUT", "AUT", "AUT", "AUT", "AUT", "AUT", "AUT","PRT","PRT","PRT","PRT","PRT"), 
                    Country2 = c("PRT", "CHE", "NOR", "SWE", "ISL", "DNK", "GBR","CHE","NOR","SWE", "ISL","DNK"),
                    stringsAsFactors = FALSE)     

and continue like this for every country dyad within the group. I would like to find a clean and concise way to do so

Thank you very much in advance for your help


Solution

  • For future reference, I think that the solution is the following

    final <- df %>% group_by(Number, year)%>%  expand(Country1 = Country, Country2= Country) %>% filter(Country1!=Country2)