-1

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 :)

anabelbs
  • 33
  • 4
  • Can you post a small toy example? – Vons Feb 28 '21 at 21:00
  • I have edited my question – anabelbs Feb 28 '21 at 21:31
  • 1
    @anabelbs Please include sample data as text; we cannot (easily) extract information from screenshots. The post [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) contains a lot of information on how to improve the quality of questions and what the community here on SO expects to be included in your post. PS. Don't be discouraged by the downvote (not mine). – Maurits Evers Feb 28 '21 at 22:55

1 Answers1

1

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
Vons
  • 3,277
  • 2
  • 16
  • 19