0

I'm accessing data in R through Haver. Haver has multiple databases, and within these databases, multiple indicators for multiple countries. With the code below, I'm able to extract an indicator (bank liquidity ratios) for multiple countries, for a specified time period (2019-2020), from a specified database ("ifs").

#specify the database - in this case "ifs"
list_ifs_meta <- haver.metadata(dat="ifs")

#specify the indicator - in this case "a...claa" (the dots replacing the country code so that I get the indicator for all countries)
list_liq_meta <- list_ifs_meta[ grepl("a...claa", list_ifs_meta$code)]

# data extraction from Haver for the specified indicator out of the specified database
df_liq <- haver.data(
            list_liq_meta, 
            start = as.Date("01 01 2019", format = "%d %m %Y"), 
            end = as.Date("01 01 2020", format = "%d %m %Y"), 
            frequency = 'annual'
            )

#labeling the columns with the country code
colnames(df_liq) <- haver.datamd(df_liq)$geography1

#transforming the output to a data.frame
df_liq <- as.data.frame(df_liq)

Now, while this works well, it's a bit cumbersome to repeat and adjust this code for each and every data extraction. Instead, I defined a data.frame, where I specified the information for multiple data extractions.

database <- c("ifs", "glsector", "esg")
indicator <- c("a...claa$", "..owncs$", "x...opr$")
startd <- c("01-01-2019", "01-01-2020", "01-01-2020")
endd <- c("01-01-2020", "01-06-2021", "01-06-2021")
freq <- c("annual", "daily", "daily")

df_data_req <- data.frame(database, indicator, startd, endd, freq)

From here, I would wish to run a for loop running through the rows of the data.frame specified above (df_data_req). However, I believe I fail to define names of lists within the loop. For instance, for defining the database, the loop would look at follows:

for (i in 1:nrow(df_data_req)) {
  list_meta_[[i]] <- haver.metadata(dat = as.character(df_data_req$database[i]))
  }

Ideally, this loop would produce three lists (list_meta_ifs, list_meta_glsector, list_meta_esg). However, I only get an error, saying that the object 'list_meta_' could not be found. Hence, my question ultimately is: how can I define the name of a list, within a for loop, based in inputs out of a data.frame?

Could anybody help? Many thanks in advance!

WannaPi
  • 1
  • 1
  • 1
    Please provide us with a small, reproducible code snippet that we can copy and paste to better understand the issue and test possible solutions. You can share datasets with `dput(YOUR_DATASET)` or smaller samples with `dput(head(YOUR_DATASET))`. (See [this answer](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) for detailed instructions.) – ktiu Jun 08 '21 at 09:11

0 Answers0