I need an alternative to the index(,match,match) function in r

55 Views Asked by At

I have two datasets, data_issuer and ESG_data.

data_issuer contains bond pricing observations, the identity of the bond issuer is given with the EquityISIN variable. The same EquityISIN occurs multiple times in the dataset for two reasons 1. for each bond there are multiple pricing observations 2. each issuer can issue multiple bonds The data_issuer dataset also contains a variable Year. which shows the year of the bond observation.

The first column of the ESG_data dataset, ISIN CODE, contains EquityISIN’s of public listed companies. The rest of the variables are years from 2000 til 2021. For clarity: the column names are 2000, 2001, 2002,.... 2021.

If I want to extract the ESG score of a specific company from the ESG_data dataset I look at the EquityISIN and the year, the intersection of the row and column holds the ESG score I am looking for.

I want to write a code which allows me to add a new variable to the data_issuer dataset called ESGscore. The ESG score has to be extracted from the ESG_data dataset using the EquityISIN value and the Year value.

I tried the following code but it returns an error:

merged_data <- merge(
  data_issuer, 
  ESG_data, 
  by.x = c("EquityISIN", "Year"), 
  by.y = c("ISIN CODE", "2000":"2021")
)

# Error in merge.data.frame(data_issuer, ESG_data, by.x = c("EquityISIN", : 
# 'by.x' and 'by.y' specify different numbers of columns

Also tried with the help of chat GPT but did not help unfortunately.

0

There are 0 best solutions below