Home > Software engineering >  How to loop dataframe in R
How to loop dataframe in R

Time:10-20

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))))))

CodePudding user response:

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)
  }

CodePudding user response:

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!

  • Related