-1

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

Alex
  • 1,207
  • 9
  • 25
  • The `final` dataframe provide a glimpse of how the final output should look like. because I had to write it manually and writing all of it would have taken ages. I guess one could consider the `final` data frame as the `head()` of the output. Meaning that in the real output I would like to have for every value of the variable `Number` all possible combination of dyads. – Alex Nov 21 '19 at 18:05
  • Any idea for dealing with the issue? – Alex Nov 21 '19 at 18:18

2 Answers2

2

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)
Alex
  • 1,207
  • 9
  • 25
1

Using tidyverse:

library(tidyverse)

df %>%
  full_join(df, by=c("Number", "year")) %>%
  filter(Country.x != Country.y)
Ben
  • 28,684
  • 5
  • 23
  • 45