0

I have the dataframe assets_year:

fiscalyear countryname       Assets net_margin
       <int> <chr>              <dbl>      <dbl>
1       2010 Austria      1602544072.      1.72 
2       2010 Belgium      2534519957.      0.974
3       2010 Estonia        33248259.      1.31 
4       2010 Finland      1490200498.      1.42 
5       2010 France      17137601040.      1.51 
6       2010 Germany     11553780086.      2.32

tail

fiscalyear countryname      Assets net_margin
       <int> <chr>             <dbl>      <dbl>
1       2017 Luxembourg   503785373.      0.730
2       2017 Netherlands 3810079489.      1.40 
3       2017 Portugal     504072448.      1.73 
4       2017 Slovakia      61735274.      2.49 
5       2017 Slovenia      41642423.      1.96 
6       2017 Spain       4397884239.      1.39 

Additionally, I summed up the asset values per year in another DF:

  fiscalyear `sum(Assets)`
       <int>         <dbl>
1       2010  52192928317.
2       2011  55914561036.
3       2012  52202110772.
4       2013  42418952433.
5       2014  53001352848.
6       2015  43550880007.

In order to scale net margin per asset value, I would like to cbind(...) the sum(assets) to my preexisting dataframe which is in panel format. Thus all countries have a entry for 2010, 2011 ... 2017.

chrtpmdr
  • 35
  • 6
  • 1
    Are you looking for `merge(assets_year, DF, all.x = TRUE)`? – Rui Barradas Mar 31 '22 at 13:19
  • As Rui suggests, this looks like it should be a `merge` or `join` operation, not `cbind`. I suggest your look at https://stackoverflow.com/q/1299871/3358272 and https://stackoverflow.com/q/5706437/3358272 for a description, and then try Rui's code to verify it is what you need. (Please report back if it works; if it does not, I suggest you try to explain about its output is incorrect.) – r2evans Mar 31 '22 at 13:21
  • 1
    a `tidy` way: `DF %>% group_by(fiscalyear) %>% mutate(asset_sum = sum(Assets))`. Alternatively you could join the tables (DF and summary) - `DF %>% left_join(summary, by = "fiscalyear")` – Eric Mar 31 '22 at 13:23
  • @Eric the first one did it for me. Perfect and simple, thanks! – chrtpmdr Mar 31 '22 at 13:44

0 Answers0