generated from realxinzhao/gcamdata-clean
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Data updates to 2022 FAOSTAT along with BYU and PCe processing updates (
#15)
1 parent
1870dac
commit 24a8d58
Showing
240 changed files
with
24,443 additions
and
10,162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
193 changes: 125 additions & 68 deletions
193
R/xfaostat_L999_CSVExport.R → R/yfaostat_GCAM_CSVExport.R
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright 2019 Battelle Memorial Institute; see the LICENSE file. | ||
|
||
#' module_yfaostat_SUA_FBS_CSVExport | ||
#' | ||
#' Generate supply utilization balance in primary equivalent | ||
#' | ||
#' @param command API command to execute | ||
#' @param ... other optional parameters, depending on command | ||
#' @return Depends on \code{command}: either a vector of required inputs, a vector of output names, or (if | ||
#' \code{command} is "MAKE") all the generated outputs | ||
#' @details This chunk compiles balanced supply utilization data in primary equivalent in GCAM region and commodities. | ||
#' @importFrom assertthat assert_that | ||
#' @importFrom dplyr summarize bind_rows filter if_else inner_join left_join mutate rename select n group_by_at | ||
#' first case_when vars | ||
#' @importFrom tibble tibble | ||
#' @importFrom tidyr complete drop_na gather nesting spread replace_na | ||
#' @author XZ Sep2024 | ||
module_yfaostat_SUA_FBS_CSVExport <- function(command, ...) { | ||
|
||
MODULE_INPUTS <- | ||
c(FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_SUA_PrimaryEquivalent"), | ||
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "SUA_item_code_map"), | ||
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_FAO_iso_reg"), | ||
"GCAM_APE_after2010", | ||
"Bal_new_all", | ||
"FBS_wide", | ||
"FAO_Food_Macronutrient_All") | ||
|
||
MODULE_OUTPUTS <- | ||
c(CSV = "GCAMFAOSTAT_DataArchive_SUA") | ||
|
||
if(command == driver.DECLARE_INPUTS) { | ||
return(MODULE_INPUTS) | ||
} else if(command == driver.DECLARE_OUTPUTS) { | ||
return(MODULE_OUTPUTS) | ||
} else if(command == driver.MAKE) { | ||
|
||
year <- value <- Year <- Value <- FAO_country <- iso <- NULL # silence package check. | ||
|
||
all_data <- list(...)[[1]] | ||
|
||
|
||
Curr_Envir <- environment() | ||
|
||
|
||
|
||
if (OUTPUT_Export_CSV == "DataArchive_SUA_FBS") { | ||
|
||
# Load required inputs ---- | ||
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) | ||
|
||
|
||
# SUA and FBS ---- | ||
## *SUA ---- | ||
|
||
Bal_new_all %>% filter(value != 0.0) %>% | ||
transmute(area_code, item_code, element, year, value) %>% | ||
add_title("GCAMFAOSTAT_DataArchive_SUA") %>% | ||
add_units("1000 tonnes") %>% | ||
add_comments("gcamfaostat Export CSV") %>% | ||
add_precursors("Bal_new_all") -> | ||
GCAMFAOSTAT_DataArchive_SUA | ||
|
||
|
||
output_csv_data( | ||
gcam_dataset = GCAMFAOSTAT_DataArchive_SUA, | ||
out_filename = "GCAMFAOSTAT_DataArchive_SUA", | ||
col_type_nonyear = "iifin", | ||
title = "Supply_utilization_accounting for all FAO items in FAOSTAT_Hist_Year_FBS", | ||
unit = "1000 tonnes", | ||
code = "SCL", | ||
description = "Data is compiled and generated by gcamfaostat. Data is balanced in trade, supply_utilization, and storage", | ||
out_dir = DIR_OUTPUT_CSV, | ||
GZIP = F) | ||
|
||
rm(Bal_new_all) | ||
|
||
|
||
} | ||
else { | ||
|
||
lapply(MODULE_OUTPUTS[MODULE_OUTPUTS %>% names() == "CSV"], | ||
function(output){ | ||
assign(output, empty_data() %>% | ||
add_title(output), envir = Curr_Envir) | ||
}) | ||
} | ||
|
||
|
||
return_data(MODULE_OUTPUTS) | ||
|
||
} else { | ||
stop("Unknown command") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,378 @@ | ||
Bal_new_all %>% filter(value != 0) %>% | ||
spread(year, value) %>% | ||
write.csv("B.csv") | ||
GCAM_APE_after2010 %>% | ||
spread(year, value) %>% | ||
write.csv("A.csv") | ||
|
||
|
||
# This module compares the new PCE data vs FAO FBS | ||
|
||
MODULE_INPUTS <- | ||
c("GCAM_APE_after2010", | ||
"Bal_new_all", | ||
"FAO_Food_Macronutrient_All", | ||
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_SUA_PrimaryEquivalent"), | ||
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "SUA_item_code_map"), | ||
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_FAO_iso_reg") ) | ||
|
||
MODULE_INPUTS %>% load_from_cache() -> all_data | ||
|
||
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE) | ||
|
||
GCAM_APE_after2010 %>% distinct(GCAM_commodity) | ||
|
||
Bal_new_all %>% filter(value != 0) | ||
GCAM_APE_after2010 %>% filter(value != 0) | ||
|
||
|
||
|
||
SUA_item_code_map %>% select(item, item_code) -> SUA_item_code_map | ||
FAO_Food_Macronutrient_All -> FAO_Food_Macronutrient_rate | ||
|
||
|
||
|
||
Bal_new_all %>% filter(value != 0.0) %>% | ||
transmute(area_code, item_code, element, year, value) -> | ||
GCAMFAOSTAT_SUA | ||
|
||
# Goal | ||
# Compare SUA & FBS for wheat/corn at global & regional scales | ||
# But adding details | ||
|
||
|
||
|
||
Mapping_SUA_PrimaryEquivalent %>% | ||
left_join_error_no_match(SUA_item_code_map %>% select(item, item_code) %>% | ||
rename(sink_item_code = item_code), by=c("sink_item" = "item")) %>% | ||
left_join_error_no_match(SUA_item_code_map %>% select(item, item_code) %>% | ||
rename(source_item_code = item_code), by=c("source_item" = "item")) %>% | ||
mutate(APE_comm = as.factor(APE_comm)) -> | ||
Mapping_SUA_PrimaryEquivalent_ID | ||
|
||
|
||
Mapping_SUA_PrimaryEquivalent_ID %>% | ||
distinct(APE_comm) %>% pull | ||
|
||
# china mainland 41 | ||
# usa 231 | ||
AC = 231 | ||
APE_COMM_NAME <- "Wheat" | ||
|
||
|
||
Mapping_SUA_PrimaryEquivalent_ID %>% | ||
filter(APE_comm == APE_COMM_NAME) %>% | ||
distinct(sink_FBS_item) %>% pull -> | ||
FAO_FBS_COMM_NAME | ||
|
||
Mapping_SUA_PrimaryEquivalent_ID %>% | ||
filter(APE_comm == APE_COMM_NAME) %>% | ||
select(sink_item_code, source_item_code) %>% unlist %>% unique() -> | ||
SUACode | ||
|
||
GCAMFAOSTAT_SUA %>% | ||
filter(item_code %in% SUACode) -> | ||
GCAMFAOSTAT_SUA_sector | ||
|
||
GCAMFAOSTAT_SUA_sector %>% | ||
filter(area_code== AC) %>% | ||
group_by_at(vars(-area_code, -value)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
left_join_error_no_match(SUA_item_code_map) %>% select(-item_code) %>% | ||
filter(year == 2020) %>% spread(item, value) -> | ||
GCAMFAOSTAT_SUA_sector1 | ||
|
||
|
||
FAO_Food_Macronutrient_rate %>% | ||
filter(area_code== AC) %>% | ||
filter(item_code %in% SUACode) %>% | ||
group_by_at(vars(year, item_code, element = macronutrient)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
left_join_error_no_match(SUA_item_code_map) %>% select(-item_code) %>% | ||
filter(year == 2020) %>% | ||
spread(item, value) %>% | ||
bind_rows( | ||
GCAMFAOSTAT_SUA_sector1 | ||
) -> | ||
GCAMFAOSTAT_SUA_sector1_2020 | ||
|
||
|
||
|
||
|
||
|
||
"FBS_wide" %>% load_from_cache() %>% first() -> FBS_wide | ||
|
||
FBS_wide %>% gather_years() %>% | ||
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>% | ||
FAOSTAT_AREA_RM_NONEXIST() -> FBS | ||
|
||
|
||
FBS %>% | ||
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>% | ||
# keep only balance items | ||
filter(!element_code %in% c(645, 664, 674, 684)) %>% | ||
# simplify elements and make them consistent with SUA | ||
mutate(element = gsub(" Quantity| supply quantity \\(tonnes\\)| \\(non-food\\)", "", element), | ||
element = replace(element, element == "Losses", "Loss"), | ||
element = replace(element, element == "Processing", "Processed")) %>% | ||
# convert units back to tonnes first since FBS originally used 1000 tons | ||
mutate(value = value) -> | ||
FBS1 | ||
|
||
|
||
FBS1 %>% filter(year == 2020) %>% | ||
filter(area_code== AC) %>% | ||
filter(item %in% FAO_FBS_COMM_NAME ) %>% | ||
group_by_at(vars(-area_code, -area, -value)) %>% | ||
summarize(value = sum(value), .groups = "drop") -> | ||
FAO_FBS_Old | ||
|
||
|
||
GCAM_APE_after2010 %>% | ||
filter(year == 2020) %>% filter(region_ID == AC) %>% | ||
filter(GCAM_commodity == APE_COMM_NAME) %>% | ||
group_by_at(vars(-region_ID, -value)) %>% | ||
summarize(value = sum(value)) %>% | ||
mutate(GCAM_commodity = "SUA") %>% | ||
spread(GCAM_commodity, value) -> | ||
SUA_FBS_New | ||
|
||
FAO_FBS_Old %>% | ||
select(-element_code) %>% | ||
rename(FBS = value) %>% | ||
full_join(SUA_FBS_New) %>% | ||
full_join( | ||
GCAMFAOSTAT_SUA_sector1_2020, by = join_by(element, year) | ||
) -> Compare | ||
|
||
|
||
C %>% readr::write_csv("Maize2020_usa.csv") | ||
|
||
|
||
|
||
|
||
#---- All region ---- | ||
|
||
|
||
# china mainland 41 | ||
# usa 231 | ||
|
||
APE_COMM_NAME <- "Wheat" | ||
|
||
|
||
Mapping_SUA_PrimaryEquivalent_ID %>% | ||
filter(APE_comm == APE_COMM_NAME) %>% | ||
distinct(sink_FBS_item) %>% pull -> | ||
FAO_FBS_COMM_NAME | ||
|
||
Mapping_SUA_PrimaryEquivalent_ID %>% | ||
filter(APE_comm == APE_COMM_NAME) %>% | ||
select(sink_item_code, source_item_code) %>% unlist %>% unique() -> | ||
SUACode | ||
|
||
GCAMFAOSTAT_SUA %>% | ||
filter(item_code %in% SUACode) -> | ||
GCAMFAOSTAT_SUA_sector | ||
|
||
GCAMFAOSTAT_SUA_sector %>% | ||
group_by_at(vars(-value)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
left_join_error_no_match(SUA_item_code_map) %>% select(-item_code) %>% | ||
filter(year == 2020) %>% | ||
spread(item, value, fill = 0) -> | ||
GCAMFAOSTAT_SUA_sector1 | ||
|
||
|
||
FAO_Food_Macronutrient_rate %>% | ||
filter(item_code %in% SUACode) %>% | ||
group_by_at(vars(area_code, year, item_code, element = macronutrient)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
left_join_error_no_match(SUA_item_code_map) %>% select(-item_code) %>% | ||
filter(year == 2020) %>% | ||
mutate(value = if_else(element %in% c("MtProtein", "MtFat"), value * 1000, value)) %>% | ||
mutate(element = replace(element, element == "MKcal", "Calorie"), | ||
element = replace(element, element == "MtProtein", "Protein"), | ||
element = replace(element, element == "MtFat", "Fat")) %>% | ||
spread(item, value) %>% | ||
bind_rows( | ||
GCAMFAOSTAT_SUA_sector1 | ||
) -> GCAMFAOSTAT_SUA_sector1_2020 | ||
|
||
|
||
|
||
|
||
|
||
"FBS_wide" %>% load_from_cache() %>% first() -> FBS_wide | ||
|
||
FBS_wide %>% gather_years() %>% | ||
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>% | ||
FAOSTAT_AREA_RM_NONEXIST() -> FBS | ||
|
||
|
||
FBS %>% | ||
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>% | ||
# keep only balance items | ||
filter(!element_code %in% c(645, 664, 674, 684)) %>% | ||
# simplify elements and make them consistent with SUA | ||
mutate(element = gsub(" Quantity| supply quantity \\(tonnes\\)| \\(non-food\\)", "", element), | ||
element = replace(element, element == "Losses", "Loss"), | ||
element = replace(element, element == "Processing", "Processed")) %>% | ||
# convert units back to tonnes first since FBS originally used 1000 tons | ||
mutate(value = value) -> | ||
FBS1 | ||
|
||
|
||
FBS1 %>% filter(year == 2020) %>% | ||
filter(item %in% FAO_FBS_COMM_NAME ) %>% | ||
group_by_at(vars(-area, -value)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
mutate(element = replace(element, element == "Food supply (kcal)", "Calorie"), | ||
element = replace(element, element == "Protein supply quantity (t)", "Protein"), | ||
element = replace(element, element == "Fat supply quantity (t)", "Fat")) %>% | ||
mutate(value = if_else(element %in% c("Protein", "Fat"), value / 1000, value)) %>% | ||
mutate(unit = replace(unit, is.na(unit)|unit == "t", "1000 t")) -> | ||
FAO_FBS_Old | ||
|
||
|
||
GCAM_APE_after2010 %>% | ||
filter(year == 2020) %>% | ||
filter(GCAM_commodity == APE_COMM_NAME) %>% | ||
group_by_at(vars(-value)) %>% | ||
summarize(value = sum(value)) %>% ungroup() %>% | ||
mutate(GCAM_commodity = "SUA") %>% | ||
spread(GCAM_commodity, value) %>% | ||
rename(area_code = region_ID)-> | ||
SUA_FBS_New | ||
|
||
GCAMFAOSTAT_SUA_sector1_2020 %>% | ||
gather(item, value, -area_code:-element) %>% | ||
group_by_at(vars(-value, -item)) %>% | ||
summarize(SUA_sum = sum(value), .groups = "drop") -> | ||
SUA_FBS_New_sum | ||
|
||
FAO_FBS_Old %>% | ||
select(-element_code) %>% filter(unit == "1000 t") %>% | ||
spread(element, value, fill = 0) %>% | ||
mutate(`Regional supply` = Production + Import, | ||
`Regional demand` = Export + Food + Feed + Processed + Seed + Loss + `Other uses` + `Tourist consumption`, | ||
Residuals = `Regional supply` - `Regional demand` - `Stock Variation`) %>% | ||
gather(element, value, -area_code:-year) %>% | ||
bind_rows( | ||
FAO_FBS_Old %>% select(-element_code) %>% filter(unit != "1000 t") | ||
) %>% | ||
rename(FBS = value) %>% | ||
full_join(SUA_FBS_New) %>% | ||
full_join(SUA_FBS_New_sum) %>% | ||
mutate(SUA = if_else(element %in% c("Calorie", "Protein", "Fat"), SUA_sum, SUA)) %>% | ||
full_join( | ||
GCAMFAOSTAT_SUA_sector1_2020 | ||
) -> Compare | ||
|
||
Compare %>% filter(area_code==203) %>% write.csv("A.csv") | ||
Compare %>% filter(element == "Residuals") %>% | ||
select(area_code:SUA) %>% | ||
filter(area_code == AC) | ||
|
||
Compare %>% | ||
filter(element == "Residuals") %>% | ||
select(area_code:SUA) -> A | ||
|
||
Compare %>% | ||
filter(element == "Residuals") %>% | ||
select(area_code:SUA) %>% | ||
group_by(item, year) %>% filter(!is.na(item)) %>% #filter(is.na(SUA)) | ||
summarize(FBS = sum(FBS), SUA = sum(SUA, na.rm = T)) | ||
filter(area_code == AC) | ||
|
||
Compare %>% filter(area_code == AC) -> C | ||
|
||
|
||
C %>% readr::write_csv("Maize2020_usa.csv") | ||
|
||
|
||
|
||
|
||
--- | ||
----- | ||
|
||
Mapping_SUA_PrimaryEquivalent %>% | ||
left_join_error_no_match(SUA_item_code_map %>% rename(sink_item_code = item_code), by=c("sink_item" = "item")) %>% | ||
left_join_error_no_match(SUA_item_code_map %>% rename(source_item_code = item_code), by=c("source_item" = "item")) %>% | ||
mutate(APE_comm = as.factor(APE_comm)) -> | ||
Mapping_SUA_PrimaryEquivalent_ID | ||
|
||
Mapping_SUA_PrimaryEquivalent_ID %>% | ||
filter(APE_comm == APE_COMM_NAME) %>% | ||
select(sink_item_code, source_item_code) %>% unlist %>% unique() -> SUACode | ||
|
||
GCAMFAOSTAT_SUA %>% | ||
filter(item_code %in% SUACode) -> | ||
GCAMFAOSTAT_SUA_Wheat | ||
|
||
GCAMFAOSTAT_SUA_Wheat %>% #filter(area_code== AC) %>% | ||
group_by_at(vars(-area_code, -value)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
left_join_error_no_match(SUA_item_code_map) %>% select(-item_code) %>% | ||
filter(year == 2020) %>% spread(item, value) -> | ||
GCAMFAOSTAT_SUA_Wheat1 | ||
|
||
|
||
FAO_Food_Macronutrient_rate %>% #filter(area_code== AC) %>% | ||
filter(item_code %in% SUACode) %>% | ||
group_by_at(vars(year, item_code, element = macronutrient)) %>% | ||
summarize(value = sum(value), .groups = "drop") %>% | ||
left_join_error_no_match(SUA_item_code_map) %>% select(-item_code) %>% | ||
filter(year == 2020) %>% | ||
spread(item, value) %>% | ||
bind_rows( | ||
GCAMFAOSTAT_SUA_Wheat1 | ||
) -> | ||
GCAMFAOSTAT_SUA_Wheat_2020 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
FBS %>% | ||
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>% | ||
# keep only balance items | ||
filter(!element_code %in% c(645, 664, 674, 684)) %>% | ||
# simplify elements and make them consistent with SUA | ||
mutate(element = gsub(" Quantity| supply quantity \\(tonnes\\)| \\(non-food\\)", "", element), | ||
element = replace(element, element == "Losses", "Loss"), | ||
element = replace(element, element == "Processing", "Processed")) %>% | ||
# convert units back to tonnes first since FBS originally used 1000 tons | ||
mutate(value = value) -> | ||
FBS1 | ||
|
||
|
||
FBS1 %>% filter(year == 2020) %>% | ||
filter(area_code== AC) %>% | ||
filter(item %in% FAO_FBS_COMM_NAME ) %>% | ||
group_by_at(vars(-area_code, -area, -value)) %>% | ||
summarize(value = sum(value), .groups = "drop") -> A | ||
|
||
|
||
GCAM_APE_after2010 %>% filter(year == 2020) %>% filter(region_ID == AC) %>% | ||
filter(GCAM_commodity == APE_COMM_NAME) %>% | ||
group_by_at(vars(-region_ID, -value)) %>% | ||
summarize(value = sum(value)) %>% | ||
mutate(GCAM_commodity = "SUA") %>% | ||
spread(GCAM_commodity, value) -> B | ||
|
||
A %>% select(-element_code) %>% | ||
rename(FBS = value) %>% | ||
full_join(B) %>% | ||
full_join( | ||
GCAMFAOSTAT_SUA_Wheat_2020, by = join_by(element, year) | ||
) -> C | ||
|
||
|
||
C %>% readr::write_csv("Maize2020.csv") | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.