1212# ' memory usage, the tibble is not exactly in tidy format.
1313# ' It contains the following columns:
1414# ' - `year`: The year in which the recorded event occurred.
15- # ' - `item`: Natural language name for the item that is being traded.
15+ # ' - `item_cbs_code`: FAOSTAT internal code for the item that is being traded.
16+ # ' For code details see e.g. `add_item_cbs_name()`.
1617# ' - `bilateral_trade`: Square matrix of `NxN` dimensions where `N` is the
1718# ' total number of countries being considered. The matrix row and column
1819# ' names are exactly equal and they represent country codes.
19- # ' - Row name: FAOSTAT internal code for the country that is exporting the
20- # ' item. Equivalences with ISO 3166-1 numeric can be found in the
21- # ' _Area Codes_ CSV from the zip file that can be downloaded from
22- # ' [FAOSTAT](https://www.fao.org/faostat/en/#data/FBS). TODO: Think about
23- # ' this, would be nice to use ISO3 codes but won't be enough for our
24- # ' periods
20+ # ' - Row name: The code of the country where the data is from. For code
21+ # ' details see e.g. `add_area_name()`.
2522# ' - Column name: FAOSTAT internal code for the country that is importing the
2623# ' item. See row name explanation above.
2724# '
@@ -107,7 +104,7 @@ get_bilateral_trade <- function(file_path) {
107104 cbs <- " commodity_balance_sheet" | >
108105 get_file_path() | >
109106 get_wide_cbs() | >
110- dplyr :: select(year , item , area_code , export , import )
107+ dplyr :: select(year , item_cbs_code , area_code , export , import )
111108
112109 btd <- file_path | >
113110 readr :: read_csv(show_col_types = FALSE ) | >
@@ -116,7 +113,7 @@ get_bilateral_trade <- function(file_path) {
116113 codes <- .get_all_country_codes(btd , cbs )
117114
118115 btd | >
119- .nest_by_year_item (cbs , codes ) | >
116+ .nest_by_year_item_code (cbs , codes ) | >
120117 .process_bilateral_trade(codes ) | >
121118 dplyr :: select(- total_trade )
122119}
@@ -177,14 +174,14 @@ get_bilateral_trade <- function(file_path) {
177174 btd | >
178175 dplyr :: rename_with(tolower ) | >
179176 dplyr :: mutate(
180- item = as.factor(item ),
181177 unit = ifelse(unit == " Head" , " heads" , unit ),
182178 from_code = ifelse(element == " Export" , area_code , area_code_p ),
183179 to_code = ifelse(element == " Export" , area_code_p , area_code ),
184180 dplyr :: across(c(year , from_code , to_code ), as.integer )
185181 ) | >
182+ add_item_cbs_code(name_column = " item" ) | >
186183 .prefer_flow_direction(" Export" ) | >
187- dplyr :: select(year , from_code , to_code , item , unit , value )
184+ dplyr :: select(year , from_code , to_code , item_cbs_code , unit , value )
188185}
189186
190187# Keep all rows with preferred direction (Import, Export)
@@ -196,7 +193,7 @@ get_bilateral_trade <- function(file_path) {
196193 bilateral_trade | >
197194 dplyr :: anti_join(
198195 preferred_direction ,
199- by = c(" from_code" , " to_code" , " year" , " item " )
196+ by = c(" from_code" , " to_code" , " year" , " item_cbs_code " )
200197 ) | >
201198 dplyr :: bind_rows(preferred_direction )
202199}
@@ -240,7 +237,7 @@ get_bilateral_trade <- function(file_path) {
240237 }
241238}
242239
243- .nest_by_year_item <- function (btd , cbs , codes ) {
240+ .nest_by_year_item_code <- function (btd , cbs , codes ) {
244241 cbs <- cbs | >
245242 dplyr :: mutate(area_code = factor (area_code , levels = codes ))
246243
@@ -254,53 +251,52 @@ get_bilateral_trade <- function(file_path) {
254251 .filter_only_items_in_cbs(cbs ) | >
255252 tidyr :: nest(
256253 bilateral_trade = c(from_code , to_code , value ),
257- .by = c(year , item )
254+ .by = c(year , item_cbs_code )
258255 ) | >
259- dplyr :: left_join(.get_nested_cbs(cbs , codes ), c(" year" , " item " ))
256+ dplyr :: left_join(.get_nested_cbs(cbs , codes ), c(" year" , " item_cbs_code " ))
260257}
261258
262259.get_nested_cbs <- function (cbs , codes ) {
263260 cbs | >
264- dplyr :: mutate(item = as.factor(item )) | >
265261 .complete_total_trade(codes ) | >
266- dplyr :: group_by(year , item ) | >
262+ dplyr :: group_by(year , item_cbs_code ) | >
267263 .balance_total_trade() | >
268264 dplyr :: ungroup() | >
269265 tidyr :: nest(
270266 total_trade = c(
271267 area_code , export , import , balanced_export , balanced_import
272268 ),
273- .by = c(year , item )
269+ .by = c(year , item_cbs_code )
274270 )
275271}
276272
277273.complete_total_trade <- function (total_trade , codes ) {
278274 df_codes <- tibble :: tibble(area_code = codes )
279275 combs <- total_trade | >
280- dplyr :: distinct(year , item ) | >
276+ dplyr :: distinct(year , item_cbs_code ) | >
281277 dplyr :: cross_join(df_codes )
282278
283279 total_trade | >
284- dplyr :: right_join(combs , by = c(" year" , " item " , " area_code" )) | >
280+ dplyr :: right_join(combs , by = c(" year" , " item_cbs_code " , " area_code" )) | >
285281 tidyr :: replace_na(list (export = 0 , import = 0 ))
286282}
287283
288284.filter_only_items_in_cbs <- function (btd , cbs ) {
289285 btd_items <- btd | >
290- dplyr :: pull(item ) | >
286+ dplyr :: pull(item_cbs_code ) | >
291287 unique() | >
292288 sort()
293289
294290 cbs_items <- cbs | >
295- dplyr :: pull(item ) | >
291+ dplyr :: pull(item_cbs_code ) | >
296292 unique() | >
297293 sort()
298294
299295 # TODO: Also include these (need total export/import reports)
300296 items_not_in_cbs <- btd_items [! btd_items %in% cbs_items ]
301297
302298 btd | >
303- dplyr :: filter(! item %in% items_not_in_cbs )
299+ dplyr :: filter(! item_cbs_code %in% items_not_in_cbs )
304300}
305301
306302.get_all_country_codes <- function (btd , cbs ) {
0 commit comments