0

I am pretty new to R, so I am sorry in advance. Currently I am trying to add the columns of one table to another table. My first table contains general and financial data of thousands of different companies. The other table contains the tax rate of each country.

Table 1:

Company Name Year Country Total Assets EBIT EBITDA ...
ABC Company 2017 Germany 75,000,000 25,000,000 35,000,000
ABC Company 2018 Germany 70,000,000 20,000,000 17,000,000
DEF Company 2017 Germany 30,000,000 5,000,000 20,000,000
DEF Company 2018 Germany 35,000,000 10,000,000 15,000,000

Table 2:

Country Year Tax rate
Germany 2017 30%
Germany 2018 30%

So I want to add the tax rate of each country to every row of table one, if t1$country=t2$country and t1$country=t2$country. But I need to match a row from table 2 multiple times with rows from table 1, since there are quite a few companies in the same country and the same year. I guess I need to use merge or something like an inner_join, but I am not very familiar with these things. Any help is appreciated.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • `merge(tab1, tab2, by = c("Country", "Year"), all.x = TRUE)` – r2evans Nov 12 '21 at 15:33
  • Another good link for learning about joins/merges is https://stackoverflow.com/q/5706437/3358272. Certainly, though, `inner_join` is not right since you want to preserve all rows in the first table; you need `dplyr::left_join` or `merge(..., all.x=TRUE)` (equivalent). – r2evans Nov 12 '21 at 15:33
  • Thanks a lot for your help. I got it working. Can I ask you for another advice, if you do not mind? when I try to run my panel regression, fe<-df%>%plm(formula=subsidiary_ebitda~tax_attractiveness+value+subsidiary_total_assets, data = df,model = "within",index = c("subsidiary_bvd_id_number","year")) I get the message: "Error in xj[i] : invalid subscript type 'list'." – Noob in r Nov 16 '21 at 17:35
  • Since you're using `%>%`, change `plm(... data=df)` to `plm(..., data=.)`. – r2evans Nov 16 '21 at 20:37

0 Answers0