Commit 69cadd1e authored by Simon, Wolfram's avatar Simon, Wolfram
Browse files

Food supply, protein supply and imporrt export are adjusted to work with the...

Food supply, protein supply and imporrt export are adjusted to work with the bulk downloaded input file from FAOSTAT.
parent db456484
This diff is collapsed.
......@@ -2459,17 +2459,19 @@ fao_foodgroup_map = read_csv("Input_data/FBS_FAO_prot_food_supply_mapout.csv") #
# Protein supply ----------------------------------------------------------
# FBS_FAO_prot_food_supply =
FBS_FAO_prot_food_supply =
FBS_FAO_downloaded_bulk %>% as_tibble() %>% clean_names()%>%
dplyr::filter(year %in% c("2010":"2019"), #for a year average
area_code %in% country_eu, #filtering for EU+UKD countries
grepl('Protein|(kg/capita/yr)',element)) %>%
dplyr::select(-c(area_code, element_code,item_code,unit)) #%>%
pivot_wider(names_from = element,
values_from = value) #%>%
element == 'Protein supply quantity (g/capita/day)') %>%
dplyr::select(-c(area_code, element_code,item_code,unit)) %>%
pivot_wider(names_from = element,
values_from = value,
values_fn = {mean}) %>%
clean_names() %>%
replace(is.na(.), 0) %>%
left_join(fao_foodgroup_map, by="item") %>%
drop_na() %>%
dplyr::group_by(area, item, FoodGroups) %>%
dplyr::summarise(protein_gppp = mean(protein_supply_quantity_g_capita_day)) %>% ungroup() %>%
dplyr::group_by(area, FoodGroups) %>%
......@@ -2486,20 +2488,25 @@ write_csv(FBS_FAO_prot_food_supply, "Input_data/FBS_FAO_prot_food_supply3.csv")
# Food supply -------------------------------------------------------------
FBS_FAO_food_supply =
FBS_FAO_downloaded_EUgroup %>% as_tibble() %>% clean_names() %>%
FBS_FAO_downloaded_bulk %>% as_tibble() %>% clean_names()%>%
dplyr::filter(year %in% c("2010":"2019"), #for a year average
area_code %in% country_eu, #filtering for EU+UKD countries
grepl('(kg/capita/yr)',element)) %>%
dplyr::select(-c(domain_code,domain,area_code, element_code,item_code,year_code,unit,flag,flag_description)) %>%
pivot_wider(names_from = element,
values_from = value) %>%
element == 'Food supply quantity (kg/capita/yr)') %>%
dplyr::select(-c(area_code, element_code,item_code,unit)) %>%
pivot_wider(names_from = element,
values_from = value,
values_fn = {mean}) %>%
clean_names() %>%
replace(is.na(.), 0) %>%
left_join(fao_foodgroup_map, by="item") %>%
dplyr::group_by(area,item, FoodGroups) %>%
summarise(food_supply_kgcapyr=mean(food_supply_quantity_kg_capita_yr)) %>%
drop_na() %>%
dplyr::group_by(area, item, FoodGroups) %>%
dplyr::summarise(food_supply_kgcapyr = mean(food_supply_quantity_kg_capita_yr)) %>%
ungroup() %>%
dplyr::group_by(area, FoodGroups) %>%
summarise(food_supply_kgcapyr=sum(food_supply_kgcapyr)) %>%
dplyr::summarise(food_supply_kgcapyr = sum(food_supply_kgcapyr)) %>%
mutate(food_supply_kgcapyr = round(food_supply_kgcapyr,1)) %>%
mutate_all(~replace(., is.nan(.), 0)) %>%
mutate(area = recode(area, "United Kingdom of Great Britain and Northern Ireland"="United Kingdom")) %>%
rename(cifos_country = area)
......@@ -2513,9 +2520,6 @@ FBS_FAO_food_supply %>% summarise(food_supply_kgcapyr = sum(food_supply_kgcapyr)
write_csv(FBS_FAO_food_supply, "Input_data/FBS_FAO_food_supply.csv")
# Import_Export -----------------------------------------------------------
# Meeting with Renee on the 21.02.22
# Renee: that was done for validation data
......@@ -2527,30 +2531,34 @@ write_csv(FBS_FAO_food_supply, "Input_data/FBS_FAO_food_supply.csv")
# import - export - stock - non_food_uses(feed, other, seed, ... ) > negative value net_export; vice versa
# For whole EU not for countries
FBS_FAO_import_export =
FBS_FAO_downloaded_EUgroup %>% as_tibble() %>% clean_names() %>%
FBS_FAO_downloaded_bulk %>% as_tibble() %>% clean_names() %>%
dplyr::filter(year %in% c("2010":"2019"), #for a year average
area == "Europe", #filtering for all European countries
grepl('Import|Export',element)) %>%
dplyr::select(-c(domain_code,domain,area_code, element_code,item_code,year_code,unit,flag,flag_description)) %>%
pivot_wider(names_from = element,
values_from = value) %>%
area == c('European Union (27)','United Kingdom of Great Britain and Northern Ireland'),#area == c("Eu-27", "United Kingdom of Great Britain and Northern Ireland"), #filtering for all European countries
grepl('Import|Export|Feed|Stock|non-food|Tourist|Seed',element)) %>%
dplyr::select(-c(area_code, element_code,item_code,unit)) %>%
pivot_wider(names_from = element,
values_from = value,
values_fn = {mean}) %>%
clean_names() %>%
replace(is.na(.), 0) %>%
left_join(fao_foodgroup_map, by="item") %>%
dplyr::group_by(FoodGroups) %>%
dplyr::summarise(Import_ton = mean(import_quantity),
Export_ton = mean(export_quantity)) %>%
dplyr::mutate_if(is.numeric, ~ . * 1000)
write_csv(FBS_FAO_import_export, "Input_data/FBS_FAO_import_export.csv")
# Testing the amount of proteins we get: 76.4 g protein per day with the weighted mean calculation
FBS_FAO_prot_food_supply %>% summarise(prot = sum(protein_gppp))
write_csv(FBS_FAO_prot_food_supply, "FBS_FAO_prot_food_supply.csv")
drop_na() %>%
dplyr::group_by(FoodGroups, item, area) %>%
dplyr::summarise(across(import_quantity:tourist_consumption, mean)) %>%
ungroup() %>% dplyr::group_by(FoodGroups) %>%
dplyr::summarise(across(import_quantity:tourist_consumption, sum)) %>%
dplyr::mutate_if(is.numeric, ~ . * 1000) %>%
dplyr::mutate(net_import_export = import_quantity-(export_quantity+stock_variation+feed+seed+other_uses_non_food+tourist_consumption),
net_export = case_when(net_import_export < 0 ~ net_import_export * -1,
TRUE~0),
net_import = case_when(net_import_export > 0 ~ net_import_export,
TRUE~0)) %>%
dplyr::rename(export_ton = export_quantity,
import_ton = import_quantity) %>%
relocate(export_ton, .after = "import_ton")
write_csv(FBS_FAO_import_export, "Input_data/FBS_FAO_import_export2.csv")
# Crop_others sheet -------------------------------------------------------
......
......@@ -12,7 +12,7 @@ knitr::opts_knit$set(root.dir = 'C:/Users/simon083/OneDrive - Wageningen Univers
## Loading packages
```{r, echo=FALSE, message=FALSE}
```{r, echo=FALSE, message=FALSE, warning=FALSE}
#Setting the library path
.libPaths("C:/Wolfram_Admin/R-4.1.1/library")
#install.packages(c("odbc","DBI", "RSQLite", "dbplyr", "tidyverse"))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment