Search code examples
rjoinmergetidyversepanel

R- How can I merge two tables with the same variables for two different periods?


I have two tables for two different periods with the same variables:household_id, size, n_adults, n_kids, household_income: for 2018 enter image description here

for 2019 enter image description here

I would like to convert it into a new panel data table with all the information for the two periods, so basically for each household, I would like to have two rows one for each period with all the information.

enter image description here Thank you in advance :)


Solution

  • This can be accomplished using the bind_rows function in dplyr.

    A="
    Household_id Year size n_adults n_kids household_income
    1 2018 6 4 2 35.000
    2 2018 4 2 2 45.000
    3 2018 3 2 1 50.000"
    B="
    Household_id Year size n_adults n_kids household_income
    1 2019 7 4 3 40.000
    2 2019 4 2 2 60.000
    3 2019 4 2 2 50.000"
    data_2018=read.table(text=A, header=TRUE)
    data_2019=read.table(text=B, header=TRUE)
    library(dplyr)
    df=bind_rows(data_2018, data_2019)
    

    Output:

    > print(df)
      Household_id Year size n_adults n_kids household_income
    1            1 2018    6        4      2               35
    2            2 2018    4        2      2               45
    3            3 2018    3        2      1               50
    4            1 2019    7        4      3               40
    5            2 2019    4        2      2               60
    6            3 2019    4        2      2               50