0

I want to get data from IMF.However the API data is limited Therefor I get the data by continent.

How to loop the dateframe? (The data can get from "Before loop part",load data from api) The reference cannot work.https://stackoverflow.com/questions/25284539/loop-over-a-string-variable-in-r

Before the loop

library(imfr)
library(countrycode)
data(codelist)
country_set <- codelist
country_set<- country_set %>% 
  select(country.name.en , iso2c, iso3c, imf, continent, region) %>% filter(!is.na(imf) & !is.na(iso2c))
africa_iso2<- country_set$iso2c[country_set$continent=="Africa"]
asia_iso2<- country_set$iso2c[country_set$continent=="Asia"]
americas_iso2<- country_set$iso2c[country_set$continent=="Americas"]
europe_iso2<- country_set$iso2c[country_set$continent=="Europe"]
oceania_iso2<- country_set$iso2c[country_set$continent=="Oceania"]

loop part

continent <- c("africa", "asia", "americas","europe","oceania")
for(i in 1:length(continent)){
  var <- paste0("gdp_nsa_xdc_", continent[i])
  var1 <- paste0(continent[i],"_iso2")
  [[var]]<- imf_data(database_id = "IFS" , indicator = c("NGDP_NSA_XDC"),country =[[var1]],start = 2010, end = 2022,return_raw = TRUE)
  [[var]]<- [[var]]$CompactData$DataSet$Series
  }

data sample is

list(CompactData = list(`@xmlns:xsi` = "http://www.w3.org/2001/XMLSchema-instance", 
    `@xmlns:xsd` = "http://www.w3.org/2001/XMLSchema", `@xsi:schemaLocation` = "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message https://registry.sdmx.org/schemas/v2_0/SDMXMessage.xsd http://dataservices.imf.org/compact/IFS http://dataservices.imf.org/compact/IFS.xsd", 
    `@xmlns` = "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message", 
    Header = list(ID = "18e0aeae-09ec-4dfe-ab72-60aa16aaea84", 
        Test = "false", Prepared = "2022-10-19T12:02:28", Sender = list(
            `@id` = "1C0", Name = list(`@xml:lang` = "en", `#text` = "IMF"), 
            Contact = list(URI = "http://www.imf.org", Telephone = "+ 1 (202) 623-6220")), 
        Receiver = list(`@id` = "ZZZ"), DataSetID = "IFS"), DataSet = list(
        `@xmlns` = "http://dataservices.imf.org/compact/IFS", 
        Series = list(`@FREQ` = "Q", `@REF_AREA` = "US", `@INDICATOR` = "NGDP_NSA_XDC", 
            `@UNIT_MULT` = "6", `@TIME_FORMAT` = "P3M", Obs = structure(list(
                `@TIME_PERIOD` = c("2020-Q1", "2020-Q2", "2020-Q3", 
                "2020-Q4", "2021-Q1", "2021-Q2", "2021-Q3", "2021-Q4", 
                "2022-Q1", "2022-Q2"), `@OBS_VALUE` = c("5254152", 
                "4930197", "5349433", "5539370", "5444406", "5784816", 
                "5883177", "6203369", "6010733", "6352982")), class = "data.frame", row.names = c(NA, 
            10L))))))
6
  • You may need assign instead of <- i.e. assign(var, imf_data(... Commented Oct 19, 2022 at 15:29
  • @akrun assign(var,var$CompactData$DataSet$Series) is not work Commented Oct 19, 2022 at 15:36
  • The var is character. if you need the value, you would need get i.e. get(var)$CompactData. Your example is not reproducible so not able to test Commented Oct 19, 2022 at 15:37
  • As I mentioned in the comment above, var is object name in character. You need to wrap with get to return the value Commented Oct 19, 2022 at 15:37
  • @akrun the data is load from IMF, can get from "Before loop" thank you for your help Commented Oct 19, 2022 at 15:39

2 Answers 2

1

I suggest you create a list first, to which you will assign the value you want your loop to create. The following code creates a named list, and then at the end of the loop, assigns the value of each iteration to that named list:

continent <- 
  sapply(c("africa", "asia", "americas","europe","oceania"),
         c, simplify = FALSE, USE.NAMES = TRUE)

for(i in seq_len(length(continent))) {
  var <- paste0("gdp_nsa_xdc_", continent[i])
  var1 <- get(paste0(continent[i],"_iso2"))
  var <- imf_data(database_id = "IFS" , indicator = c("NGDP_NSA_XDC"),
                  country = var1, start = 2010, end = 2022, 
                  return_raw = TRUE)
  continent[[i]] <- var$CompactData$DataSet$Series
}

I don't necessarily understand the double brackets around [[var]]. Let me know if my answer does not correspond to what you were looking for!

Sign up to request clarification or add additional context in comments.

5 Comments

Just updated with @akrun's get() insight. Indeed without reproducible example, harder to test!
the data is load from api. there for can get from "Before loop" part
This piece of code should work after the first piece of code you made available (loading libraries and creating the x_iso2). The output will be stored in the continent list.
how to unnest the list??
You can index the double bracket [[]]. Like continent[[1]]
1

We could use assign to create objects in the global env

for(i in 1:length(continent)){
  var <- paste0("gdp_nsa_xdc_", continent[i])
  var1 <- paste0(continent[i],"_iso2")
  assign(var, imf_data(database_id = "IFS" , indicator = c("NGDP_NSA_XDC"),country =[[var1]],start = 2010, end = 2022,
    return_raw = TRUE))
  assign(var, get(var)$CompactData$DataSet$Series)
  }

7 Comments

Using get is not work.
@janeluyip did you had the data already loaded in the env
without last line is work.
@janeluyip the imfr package is not available when I try to download/install
@janeluyip i think there was a missing ) in assign in the first one
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.