Search code examples
rdataframecombinations

Create dyadic (relational) data from monadic data


I have conflict data that looks like this

conflict_ID country_code   SideA
1              1             1          
1              2             1 
1              3             0
2              4             1
2              5             0 

Now I want to make it into dyadic conflict data that looks like this (SideA=1 should be country_code_1):

conflict_ID country_code_1 country_code_2 
1              1             3          
1              2             3 
2              4             5

Can anyone point me in the right direction?


Solution

  • Here's a direct approach:

    df %>%
      filter(SideA == 1) %>%
      select(conflict_ID, country_code_1 = country_code) %>%
      left_join(
        df %>%
          filter(SideA == 0) %>%
          select(conflict_ID, country_code_2 = country_code),
        by = "conflict_ID"
      )
    #   conflict_ID country_code_1 country_code_2
    # 1           1              1              3
    # 2           1              2              3
    # 3           2              4              5
    

    Using this data:

    df = read.table(text = 'conflict_ID country_code   SideA
    1              1             1          
    1              2             1 
    1              3             0
    2              4             1
    2              5             0 ', header = T)