Home > Net >  Merging time-series df with panel data df
Merging time-series df with panel data df

Time:06-01

I want to add the following columns of of this df below:

  year   g  hhdi     inf  unemp  hpi
1  2010 4.2 1525.566 1.1   8.6  83.850
2  2011 3.9 1577.630 2.5   7.9  86.775
3  2012 0.4 1613.950 2.2   7.6  89.750
4  2013 0.4 1636.963 1.6   7.7  92.575
5  2014 2.2 1678.036 0.8   7.5  95.475
6  2015 1.5 1724.533 0.7   7.1  99.975
7  2016 2.2 1784.885 0.4   6.8 107.500
8  2017 2.7 1844.458 1.7   6.3 114.075
9  2018 1.1 1919.422 1.9   5.8 121.650
10 2019 1.1 1959.941 1.4   5.5 128.675

to the following panel data:

 Year_ qe npl_lnratio                                                               NAME   cap
7    2012  0  0.62829700                                                   Aachener Bank eG 18.18
8    2013  0  0.27315939                                                   Aachener Bank eG 19.10
9    2014  0  0.06071818                                                   Aachener Bank eG 17.11
10   2015  1 -0.13660923                                                   Aachener Bank eG 16.25
11   2016  1 -0.28913659                                                   Aachener Bank eG 15.76
12   2017  1 -0.68695033                                                   Aachener Bank eG 15.52
13   2018  1 -0.62712593                                                   Aachener Bank eG 15.79
16   2013  0  0.63553422                                           Aachener Bausparkasse AG  9.33
17   2014  0  0.37843498                                           Aachener Bausparkasse AG 13.11
18   2015  1  0.26252868                                           Aachener Bausparkasse AG 13.42
19   2016  1  0.33510288                                           Aachener Bausparkasse AG 14.07
20   2017  1  0.35341677                                           Aachener Bausparkasse AG 17.31
21   2018  1  0.35543734                                           Aachener Bausparkasse AG 19.00
22   2019  1  0.34700814                                           Aachener Bausparkasse AG 28.48
114  2010  0  1.64156954                                             AKA Ausfuhrkredit-GmbH 18.10
115  2011  0  0.44880167                                             AKA Ausfuhrkredit-GmbH 19.99
116  2012  0  0.06958573                                             AKA Ausfuhrkredit-GmbH 21.55
118  2014  0 -0.14343804                                             AKA Ausfuhrkredit-GmbH 18.74
119  2015  1 -0.15527808                                             AKA Ausfuhrkredit-GmbH 18.21
120  2016  1 -1.08621424                                             AKA Ausfuhrkredit-GmbH 16.91
121  2017  1 -1.44157456                                             AKA Ausfuhrkredit-GmbH 19.46
122  2018  1 -1.74754792                                             AKA Ausfuhrkredit-GmbH 17.04
134  2010  0  1.72536067                                              akf bank GmbH & Co KG 10.95
135  2011  0  1.71238396                                              akf bank GmbH & Co KG  9.90
136  2012  0  1.11063624                                              akf bank GmbH & Co KG 10.19
137  2013  0  1.51679825                                              akf bank GmbH & Co KG  9.88
139  2014  0  1.26395575                                              akf bank GmbH & Co KG 10.12
140  2015  1  1.25142651                                              akf bank GmbH & Co KG 11.39
144  2017  1  0.81729478                                              akf bank GmbH & Co KG 12.69
146  2018  1  1.15611067                                              akf bank GmbH & Co KG 13.68
147  2019  1  1.22488125                                              akf bank GmbH & Co KG 13.82

So that for every year in the panel data the corresponding g, hhdi, inflation, unemp and hpi are incorporated.

I tried working with a for loop where I check whether the years from both df correspond, but I can't make it work.

df <- data.frame()
for (i in timeseries$year) {
  for (j in panel$Year_) {
    df <- cbind(df, c())
  }
  

CodePudding user response:

Change column name in second data frame from Year_ to Year do the saame for the first df (year to Year).

merge(x=df2, y=df1, by='Year', all.x = TRUE)
  • Related