Home > Software design >  How do I iterate over a function in R?
How do I iterate over a function in R?

Time:09-29

I am a beginner to R attempting to do some analysis of UN Comtrade data using the 'comtradr' package.

For background, the package calls data from the Comtrade database (e.g. exports of fish from China for 1992 - 2021).

I know what I want to do but am struggling to work out how to do it.

In plain language, for countries in a list (country_list as below) I want to extract import data and then extract export data using the ct_search function.

Then, I hope to select relevant columns and plot each country's import / export in two separate line graph plots.

country_list <- list("China", "United States", "India") -- this should fill the reporters field in the ct_search function.

trade_list <- list("exports", "imports") -- this should fill the trade_direction field in the ct_search function.

I can do this for a single country as below, and could copy-paste the code for each country, but I know that's not efficient.

I'm guessing it's a mix of creating a function for loops but I don't know how to do that in R .. can anyone give an example? Is it straightforward? So ideally the output would be six plots -- an import and then an export plot for China, the US and India.

Any help much appreciated!

--

library(tidyverse)
library(comtradr)

df <- ct_search(
  reporters = "China", 
  partners = "World",
  trade_direction = "exports",
  freq = "annual",
    commod_codes = "030520",
  type = "goods"
)

df2 <- df %>%
  select(year, qty, trade_value_usd)

ggplot(data = df2, aes(x = year, y = qty))  
  geom_line(size = 1)  
  geom_point(size = 2)  
  theme_bw()

CodePudding user response:

  1. You don't have to loop over your countries and trade directions to get your data as ct_search allows to pass character vectors (!!), i.e. use c instead of list. Note that e.g. for the "United States" you have to use "USA".

  2. To create your plots for each country you could split your data by country, put your plotting code inside a function and use e.g. purrr::imap to loop over the list of country data. Using imap makes it easy to pass the country names as a second argument to the plotting function which I add as the plot title. Instead of separate plots for imports and exports I use facet_wrap to create a plot of imports and exports for each country.

library(tidyverse)
library(comtradr)

country_list <- c("China", "United States" = "USA", "India")
trade_list <- c("exports", "imports")

df <- ct_search(
  reporters = country_list, 
  partners = "World",
  trade_direction = trade_list,
  freq = "annual",
  commod_codes = "030520",
  type = "goods"
)

df2 <- df %>%
  select(year, qty, trade_value_usd, reporter, trade_flow)

plot_fun <- function(.data, cntry) {
  ggplot(data = .data, aes(x = year, y = qty))  
    geom_line(size = 1)  
    geom_point(size = 2)  
    facet_wrap(~trade_flow)  
    theme_bw()  
    labs(title = cntry)
}

df2 %>%
  split(.$reporter) %>%
  purrr::imap(plot_fun)
#> $China

#> 
#> $India

#> 
#> $USA

  • Related