From 735c7aa37b8aae665f7f6d70921e7d76fe20b0ce Mon Sep 17 00:00:00 2001 From: "Simon, Wolfram" <wolfram.simon@wur.nl> Date: Fri, 4 Feb 2022 18:10:46 +0000 Subject: [PATCH] Conflict with the local repo --- .../Crop_cifos_DataPreparation_DB.R | 2276 ----------------- 1 file changed, 2276 deletions(-) delete mode 100644 R_to_SQL_database/Crop_cifos_DataPreparation_DB.R diff --git a/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R b/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R deleted file mode 100644 index 95ef88a..0000000 --- a/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R +++ /dev/null @@ -1,2276 +0,0 @@ -Loading packages -------------------------------------------------------- -# Loading the packages -# .libPaths("C:/Wolfram_Admin/R-4.1.2/R-4.1.2/library") -# install.packages("easypackages", dependencies = T) -# library("easypackages") -# Loading packages -easypackages::packages("odbc","DBI", "RSQLite", "dbplyr", "readxl", "fuzzyjoin", - "sqldf", "downloader", "tidyverse", "janitor", "FAOSTAT", - "validate") - -# https://www.youtube.com/watch?v=CydajdNRJOU ----------------------------- - -# con <- dbConnect(odbc::odbc(), -# .connection_string = "Driver={MySQL ODBC 8.0 Unicode Driver};", -# Server = "localhost", Database = "data", UID = "root", PWD = "154236w.S", -# Port = 3306) -# -# -# -# install.packages("quantmod"); require("quantmod") -# -# ticker = "AAPL" -# stock = getSymbols(ticker, auto.assign = FALSE) -# -# # extracting index -# idx = as.data.frame(index(stock)) -# stock = as.data.frame(cbind(idx,coredata(stock))) -# colnames(stock) = c("Date", "Open", "High", "Low", "Close", "Volume" , "Adjusted") -# head(stock) -# -# stock$Symbol = ticker -# -# -# TYPES = list(Date="date", -# Open="double(10,2)", -# High="double(10,2)", -# Low="double(10,2)", -# Close="double(10,2)", -# Volume="Int(25)", -# Adjusted="double(10,2)", -# Symbol="varchar(5)") -# -# # Writing data into the database ----------------------------------------- -# system.time( -# dbWriteTable(con, name="temp", value=stock, field.types=TYPES, row.names=FALSE)) -# -# -# # adding another table for AMZN -# ticker = "AMZN" -# stock = getSymbols(ticker, auto.assign = FALSE) -# -# # extracting index -# idx = as.data.frame(index(stock)) -# stock = as.data.frame(cbind(idx,coredata(stock))) -# colnames(stock) = c("Date", "Open", "High", "Low", "Close", "Volume" , "Adjusted") -# head(stock) -# -# stock$Symbol = ticker -# -# tail(stock) -# -# # Writing data into the database ----------------------------------------- -# system.time( -# dbWriteTable(con, name="temp", value=stock, field.types=TYPES, row.names=FALSE)) -# -# dbSendQuery(con, "insert into stk select * from temp") -# dbSendQuery(con, "drop table temp") -# -# # End connection -# dbDisconnect(con) -# -# getSymbols(data_map, auto.assign = FALSE) - -# # https://www.youtube.com/watch?v=F4HP3CXijC8 ----------------------------- -# install.packages(c("DBI", "RSQLite", "dbplyr")) -# .libPaths(.libPaths()[2]) -# library("DBI") -# library("RSQLite") -# library("dplyr") #loads dbplyr automatically -# -# portaldb = dbConnect(SQLite(), "portal.sqlite") -# portaldb = dbConnect(RSQLite::SQLite(), "portal.sqlite") -# -# -# dbListTables((con)) #shows the tables in the connection -# dbListFields(con, "stk") #gives the fields in the tables -# -# # Displaying the table in the database -- this table is not actually an R object yet. -# stk = tbl(con, "stk") -# stk -# -# stk_df = collect(stk) #this pulls the SQL table into R tbl -# -# odbc::odbc(), -# .connection_string = "Driver={MySQL ODBC 8.0 Unicode Driver};", -# Server = "localhost", Database = "data", UID = "root", PWD = "154236w.S", -# Port = 3306) - - - -# Writing dfs into the cifos_crop schema (db: CIFOS_model_WJS) ------------ - -# Connection to a structured EER diagramm -con = dbConnect(odbc::odbc(), - .connection_string = "Driver={MySQL ODBC 8.0 Unicode Driver};", - Server = "localhost", Database = "cifos_crop", UID = "root", PWD = "154236w.S", - Port = 3306) - -# Connection to a messy schema (no eer diagram) -con_crop_map = dbConnect(odbc::odbc(), - .connection_string = "Driver={MySQL ODBC 8.0 Unicode Driver};", - Server = "localhost", - Database = "crop_mapping", - UID = "root", - PWD = "154236w.S", - Port = 3306) - -# https://db.rstudio.com/getting-started/database-queries -# Loading the data sets from Database ------------------------------------- -data_map = tbl(con_crop_map, "data_map") -data_map = as_tibble(tbl(con_crop_map, "data_map")) - -# Crops ------------------------------------------------------------------- -# Issue: THis will not work anymore as the crop file on the database has been uploaded since. -crop_disag_map = as_tibble(tbl(con_crop_map, "crop_disag_map")) -# The crop dataset: Job is to add grass and get the original names back to column crop_cifos -crop = as_tibble(tbl(con, "crop")) -mapping_grasscrops = read_csv("Input_data/mapping_grasscrops_new.csv") %>% - dplyr::rename(crop_cifos = crops_cifos) %>% dplyr::select(crop_cifos) - -dat_crop = as_tibble(crop) %>% - dplyr::mutate(old_cifos_crop = crop_cifos) %>% - dplyr::mutate(crop_cifos = ifelse(!is.na(crop_name_monfreda), - crop_name_monfreda, - crop_long_spam)) %>% - dplyr::filter(!grepl("grass", crop_cifos)) %>% - bind_rows(.,mapping_grasscrops) %>% - mutate(crop_cifos = tolower(crop_cifos), - crop_cifos = gsub("\\,", "", crop_cifos), - crop_cifos = gsub(" ", "_", crop_cifos)) %>% - dplyr::mutate(crop_cifos = dplyr::recode(crop_cifos, - "tobacco_unmanufactured" = "tobacco", - "groundnuts_with_shell" = "groundnuts", - # "dark_green_vegetables " = "green_vegetables", - "other_roots_and_tubers" = "other_tubers", - # grepl("red_and_orange",crop_cifos) = "red_vegetables", - # "other_vegetables " = "other_vegetables", - "sunflower_seed" = "sunflower", - "other_cereal_crops" = "other_cereals", - "sesame_seed" = "sesame")) -# write_csv(dat_crop, "Input_data/dat_crop_with_grass.csv") - -#ISSUE: The vegetable values are behaving weird. Had to remove two spaces at the end manually in CSV. -#Be careful when changing again. -dat_crop2=read_csv("Input_data/dat_crop_with_grass.csv") %>% - dplyr::mutate(crop_cifos = dplyr::recode(crop_cifos, - "dark_green_vegetables" = "green_vegetables", - "red_and_orange_vegetables" = "red_vegetables", - "other_vegetables" = "other_vegetables")) - -write_csv(dat_crop2, "Input_data/dat_crop_with_grass_final.csv") -dat_crop_final = read_csv("Input_data/dat_crop_with_grass_final.csv") - - - -# crop_cifos = case_when(crop_cifos != is.na(crop_cifos) ~ crop_long_spam)) %>% -# dplyr::mutate(crop_cifos_2 = case_when(crop_cifos = is.na(crop_cifos) ~ crop_name_monfreda), -# TRUE ~ crop_cifos) - -# class(data_map) -# -# # Loading data into R from database - 152 rows -# # Comment: Complete! ProcOutH -> Dietary Products -# # -# procOutH_dietprod = data_map %>% -# dplyr::select(`ProcOutH...1`:DietProd) %>% -# dplyr::arrange() %>% -# drop_na() -# -# # crops - 111 rows -# procOut_all = data_map %>% -# dplyr::select(`ProcOutH...5`) %>% -# dplyr::arrange() %>% -# drop_na() -# -# # crops_co product - rows 31 -# # comments: mapping is not correct!! -# crops_coproduct = data_map %>% -# dplyr::select(`CROPS FAO...7`:`Co-Product...8`) %>% -# dplyr::arrange() %>% -# drop_na() -# -# # Crops and coproducts - 110 rows -# crops_coproduct_2 = data_map %>% -# dplyr::select(c(22,23)) %>% -# dplyr::arrange(c(1)) %>% -# drop_na() -# -# # Crops and coproducts and dietary products - 110 rows -# # # Not important! Ignore -# # crops_coproduct_dietaryprod = data_map %>% -# # dplyr::select(c(22,23,24)) %>% -# # dplyr::arrange(c(1)) %>% -# # drop_na() -# -# # Livestock - 11 rows -# livestock_co_product = data_map %>% -# dplyr::select(c(11,12)) %>% -# dplyr::arrange(LIVESTOCK) %>% -# drop_na() -# -# # Fish products - 18 rows -# Fish_coproducts = data_map %>% -# dplyr::select(c(15,16)) %>% -# dplyr::arrange(FISH) %>% -# drop_na() -# -# # #aquatic plants 14 rows -# aquatic_plants = data_map %>% dplyr::select(c(25)) %>% -# dplyr::arrange(c(1)) %>% -# drop_na() -# -# # crops-coprod-dietaryProd - 18 rows -# crops_coproduct_dietaryprod = data_map %>% -# dplyr::select(c(29, 30, 31)) %>% -# dplyr::arrange(c(1)) %>% -# drop_na() -# -# # Processing - Fraction - CropsFAO -# processingfrac_cropsfao = data_map %>% -# dplyr::select(`Processing Fraction - Products`, c(41)) -# # -# # CropsFAO - FoodProducts -# Yield_food_prod = data_map %>% dplyr::select(`CROPS FAO - yield`, `Food Products`) -# -# # Checking the crops with the dietary sheet ---------------------------------------------------- -# crop = as_tibble(tbl(con, "crop")) -dat_cifos <- read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Animal_Nutr") - -# Processing sheet -------------------------------------------------------- -Processing_sheet = read_csv("Input_data/Copy of European_CiFoS_model_data_ANITANov21.csv") -dat_crop_final = read_csv("Input_data/dat_crop_with_grass_final.csv") %>% - # Adding the grass to the old crop name columns so it will not result in NA later. - dplyr::mutate(old_cifos_crop = case_when(is.na(old_cifos_crop) ~ crop_cifos, - TRUE ~ old_cifos_crop)) - -# Fuzzy join of processing sheet and the crop map -# crop_map_cifos_procraw = -dat_proc = Processing_sheet %>% - dplyr::select(c(1:5)) %>% - stringdist_right_join(drop_na(dat_crop_final, old_cifos_crop), - by = c("ProRaw" = "old_cifos_crop"), - max_dist = 2) %>% #This results in the 59 crops (incl grass) - Good! - dplyr::select(crop_cifos, ProRaw, ProcIn,ProcOut, Value, `Dietary Products`) %>% - dplyr::rename(Proc_Raw=ProRaw) %>% clean_names() %>% - - # Adding the forage crops and grass to the processing sheet - dplyr::mutate(across(c(proc_raw, proc_in, proc_out), - ~ case_when(str_detect(crop_cifos, "for") ~ crop_cifos, - str_detect(crop_cifos, "grass") ~ crop_cifos, - crop_cifos == "tobacco" ~ crop_cifos, - crop_cifos == "seed_cotton" ~ crop_cifos, - TRUE ~ .)), - across(c(value), ~ case_when(str_detect(crop_cifos, "for") ~ 1, - str_detect(crop_cifos, "grass") ~ 1, - crop_cifos == "tobacco" ~ 1, - crop_cifos == "seed_cotton" ~ 1, - TRUE ~ .))) - -# removing disaggregated group elemetns that are not needed - -# Aim: To have only one surrogate crop per crop_group (temperate_fruits = Apples) -# First paragraph is about the aggregated crop groups -# The second paragraph is about the crops that show weird relative crops -# with no values. LIke this we can get rid of the unwanted crop rows in one go. -# Assumptions: -# - I deleted the potato starch as it had a value of 0 (Why?) - -dat_2nd_level = Processing_sheet %>% - dplyr::select(c(1:5)) %>% - stringdist_right_join(drop_na(dat_crop_final, old_cifos_crop), - by = c("ProRaw" = "old_cifos_crop"), - max_dist = 2) - -dat_2nd_level2 = anti_join(Processing_sheet,dat_2nd_level, by = "ProRaw") %>% - dplyr::select(-c(6:7)) %>% - dplyr::rename(proc_raw = ProRaw) %>% - drop_na() %>% - filter(`Dietary Products` %in% c("Sugar (Raw Equivalent)", - "Rice and products", - "Beer")) %>% - mutate(crop_cifos = c("sugar_beet", "sugar_beet", "rice", "rice", - "barley", "barley")) %>% - relocate(crop_cifos, .before = 1) %>% - clean_names() - - - -dat_group = sqldf('SELECT * FROM dat_proc WHERE - crop_cifos == "temperate_fruit" AND proc_in == "Apple" OR - crop_cifos == "other_vegetables" AND proc_in == "Cabbage_White" OR - crop_cifos == "green_vegetables" AND proc_in == "Spinach" OR - crop_cifos == "red_vegetables" AND proc_in == "Carrot_Winter" OR - crop_cifos == "other_cereals" AND proc_in == "Oat_Food" OR - crop_cifos == "other_cereals" AND proc_in == "Oat_Feed" OR - crop_cifos == "other_pulses" AND proc_in == "Bean_french" OR - crop_cifos == "other_oil_crops" AND proc_raw == "Olives" OR - crop_cifos == "other_crops" OR - crop_cifos == "other_fibres" OR - crop_cifos == "tropical_fruits" AND proc_in == "oranges_for_juice" OR - crop_cifos == "tropical_fruits" AND proc_in == "Orange" OR - crop_cifos == "other_tubers" AND proc_raw == "Yautia" OR - crop_cifos == "treenuts" AND proc_in == "Walnut" OR - - crop_cifos == "sunflower" AND proc_raw == "Sunflower seed" OR - crop_cifos == "bananas" AND proc_in == "Banana" OR - crop_cifos == "sugar_beet" AND proc_in == "Sugarbeet" OR - crop_cifos == "soybeans" AND proc_in == "Soyabean_for_oil" OR - crop_cifos == "soybeans" AND proc_in == "Soyabean_for_table" OR - crop_cifos == "maize" AND proc_in == "Maize" OR - crop_cifos == "pigeon_peas" AND proc_in == "Pea" - ') %>% as_tibble() - -# Remove the groupped crops from the mother df and bind the final dataset togehter -dat_proc_2 = dat_proc %>% - anti_join(., dat_group, by = "crop_cifos") %>% #then I delete all the rows with unwanted duplicates - bind_rows(dat_group)#then I add the nicely filtered rows and get a clean df - -# Adding missing dietary products - -# Cifos dietary products - here i filter for strings in the FAO database -dat_proc_2 %>% filter(grepl("Cocoa", dietary_products)) -# From FAO supply sheet for year 2014 - here i filter for strings in the FAO database -dat_foodsupply = read_csv("Input_data/FAOSTAT_FoodSupply_data_11-23-2021.csv") -dat_foodsupply %>% dplyr::filter(Element=="Processing") %>% - filter(grepl("Cocoa", Item)) %>% - distinct(Item) - -# df with missing dietary products -dat_missin_dietprod = dat_proc_2 %>% - dplyr::filter(is.na(dietary_products)) %>% pull(crop_cifos) - -# Adding the missing dietary prodcucts to the dataframe -dat_dietprod_comp = dat_proc_2 %>% - # Adding dietary products to the missing crops - dplyr::mutate(dietary_products = case_when( - - # Forage crops --- Fodder crops and products - crop_cifos == "beetfor" ~ "Sugar beet", - crop_cifos == "cabbagefor" ~ "Vegetables, Other", - crop_cifos == "carrotfor" ~ "Vegetables, Other", - crop_cifos == "oilseedfor" ~ "Soyabean Oil", - crop_cifos == "fornesfor" ~ "Vegetables, Other", - crop_cifos == "legumenesfor" ~ "Pulses, Other and products", - crop_cifos == "maizefor" ~ "Maize and products", - crop_cifos == "ryefor" ~ "Rye and products", # newly added to cifos - crop_cifos == "swedefor" ~ "Sugar beet", - crop_cifos == "turnipfor" ~ "Sugar beet", - crop_cifos == "vegfor" ~ "Vegetables, Other", - - # Grassy crops - crop_cifos == "grass_pasture" ~ "Roughage", #no real FAO category - crop_cifos == "cloverfor" ~ "Roughage", - str_detect(crop_cifos, "grass") ~ "Roughage", #no real FAO category - - # Food crops - crop_cifos == "cassava" ~ "Cassava and products", # newly added to cifos - crop_cifos == "coconuts" ~ "Coconuts - Incl Copra", - crop_cifos == "cow_peas_dry" ~ "Pulses, Other and products", - crop_cifos == "pearl_millet" ~ "Millet and products", # newly added to cifos - crop_cifos == "oil_palm_fruit" ~ "Palm kernels", # newly added to cifos; options: 1 Palm kernels, 2 Palmkernel Oil 3 Palm Oil - crop_cifos == "plantains" ~ "Plantains", # newly added to cifos; - crop_cifos == "seed_cotton" ~ "Fibres", #no real FAO category - crop_cifos == "sorghum" ~ "Sorghum and products",# newly added to cifos; - crop_cifos == "sugar_cane" ~ "Sugar cane", # newly added to cifos; - crop_cifos == "sweet_potatoes" ~ "Sweet potatoes", # newly added to cifos; - crop_cifos == "tea" ~ "Tea (including mate)", #newly added to cifos; - crop_cifos == "tobacco" ~ "Tobacco",#no real FAO category; newly added to cifos; - crop_cifos == "yams" ~ "Vegetables, Other", - crop_cifos == "other_crops" ~ "Pulses, Other and products", - crop_cifos == "other_fibres" ~ "Fibres",#no real FAO category;newly added to cifos; - crop_cifos == "cocoa_beans" ~ "Cocoa Beans and products", # newly added to cifos; - crop_cifos == "other_tubers" ~ "Vegetables, Other", - TRUE ~ dietary_products)) - -# Removing Nas in the columns procin and procout. Rough assumption: all prcessing fractions = 1. Names from proc_raw - -vec_proc_raw = dat_dietprod_comp %>% dplyr::filter(is.na(proc_in)) %>% pull(proc_raw) - -dat_comp = dat_dietprod_comp %>% dplyr::filter(is.na(proc_in)) %>% - tibble::add_column(na_replace = vec_proc_raw) %>% - dplyr::mutate(proc_in = na_replace, - proc_out = na_replace, - value = 1) %>% - dplyr::select(-na_replace) - -# Merging dfs - FInal processing sheet -dat_complete = dat_dietprod_comp %>% - dplyr::filter(!is.na(proc_in)) %>% - bind_rows(., dat_comp) %>% - distinct_all() %>% # tobacco was triplicated - bind_rows(dat_2nd_level2) - -# write_csv(dat_complete, "Input_data/processing_sheet_2.csv") -# dat_complete = read_csv("Input_data/processing_sheet_2.csv") - -# Isolate new dietary products -new_dietprods = dat_complete %>% - anti_join(., dat_proc_2, by="dietary_products") - -# Distinct new dietary products -new_dietprods_dist = dat_complete %>% - anti_join(., dat_proc_2, by="dietary_products") %>% - dplyr::distinct(dietary_products, .keep_all = T) - -# This is the ones that are freely added and have a correspondant entry in the FAO supply data sheet -new_dietprods_dist_FAO = new_dietprods_dist %>% - dplyr::filter(!dietary_products %in% c("Roughage", "Fibres", "Tea", "Tobacco", "Fibres")) - -# This is the ones that are freely added and have NO correspondant entry in the FAO supply data sheet -new_dietprods_dist_noFAO = new_dietprods_dist %>% - dplyr::filter(dietary_products %in% c("Roughage", "Fibres", "Tea", "Tobacco", "Fibres")) - -# Adding the missing proc outs -# 1. Identifying the missing proc-outs -# new_procout = dat_complete %>% -# dplyr::filter(!is.na(proc_out)) %>% -# distinct_all() %>% -# dplyr::anti_join(.,Processing_sheet, by=c("proc_out" = "ProcOut")) - -new_procin = dat_complete %>% - dplyr::filter(!is.na(proc_in)) %>% - distinct_all() %>% - dplyr::anti_join(.,Processing_sheet, by=c("proc_in" = "ProcIn")) - -# Conclusion: There are 33 newly added processing products that need to be processed into different processing products -# Rules: -# - Forage gets value = 1. -# - If forage crops are represented as well as food crops they have to be merged to the food crop; -# That means they should get the same procin name but then get the different procout ("wheat_feed" or so) -print(new_procin, n=33) - -print(new_procin, n=33) - -# A tibble: 33 x 6 -# crop_cifos proc_raw proc_in proc_out value dietary_products -# <chr> <chr> <chr> <chr> <dbl> <chr> -# 1 grass_pasture grass_pasture grass_pasture grass_pasture 1 Roughage -# 2 beetfor beetfor beetfor beetfor 1 Sugar beet -# 3 cabbagefor cabbagefor cabbagefor cabbagefor 1 Vegetables, Other -# 4 carrotfor carrotfor carrotfor carrotfor 1 Vegetables, Other -# 5 cloverfor cloverfor cloverfor cloverfor 1 Roughage -# 6 fornesfor fornesfor fornesfor fornesfor 1 Vegetables, Other -# 7 legumenesfor legumenesfor legumenesfor legumenesfor 1 Pulses, Other and products -# 8 maizefor maizefor maizefor maizefor 1 Maize and products -# 9 oilseedfor oilseedfor oilseedfor oilseedfor 1 Soyabean Oil -# 10 ryefor ryefor ryefor ryefor 1 Rye and products -# 11 seed_cotton seed_cotton seed_cotton seed_cotton 1 Fibres -# 12 swedefor swedefor swedefor swedefor 1 Sugar beet -# 13 tobacco tobacco tobacco tobacco 1 Tobacco -# 14 turnipfor turnipfor turnipfor turnipfor 1 Sugar beet -# 15 vegfor vegfor vegfor vegfor 1 Vegetables, Other -# 16 grass_pasture grass_pasture grass_pasture grass_pasture 1 Roughage -# 17 grass_arable grass_arable grass_arable grass_arable 1 Roughage -# 18 grass_rangeland grass_rangeland grass_rangeland grass_rangeland 1 Roughage -# 19 cassava Cassava Cassava Cassava 1 Cassava and products -# 20 cocoa_beans Cocoa beans Cocoa beans Cocoa beans 1 Cocoa Beans and products -# 21 coconuts Coconuts Coconuts Coconuts 1 Coconuts - Incl Copra -# 22 cow_peas_dry Cow peas, dry Cow peas, dry Cow peas, dry 1 Pulses, Other and products -# 23 pearl_millet Millet Millet Millet 1 Millet and products -# 24 oil_palm_fruit Oil palm fruit Oil palm fruit Oil palm fruit 1 Palm kernels -# 25 plantains Plantains Plantains Plantains 1 Plantains -# 26 sorghum Sorghum Sorghum Sorghum 1 Sorghum and products -# 27 sugar_cane Sugar cane Sugar cane Sugar cane 1 Sugar cane -# 28 sweet_potatoes Sweet potatoes Sweet potatoes Sweet potatoes 1 Sweet potatoes -# 29 tea Tea Tea Tea 1 Tea -# 30 yams Yams Yams Yams 1 Vegetables, Other -# 31 other_crops Chilies and peppers, green Chilies and peppers, green Chilies and peppers, green 1 Pulses, Other and products -# 32 other_fibres Flax fiber and tow Flax fiber and tow Flax fiber and tow 1 Fibres -# 33 other_tubers Yautia Yautia Yautia 1 Vegetables, Other - - -# Processing hardcode ----------------------------------------------------- -proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" = NA, "value" = NA, "dietary_products" = NA) %>% - - # Cocoa -- Source: from Technical Conversion Factors for Agricultural Commodities (http://countrystat.org/resources/documents/tcf.pdf) - # Assumptions: we just look at chocolate production from cocoa comodity tree up to 2nd level - # level: second level where available. - add_row(crop_cifos = "cocoa_beans" , proc_raw = "Cocoa beans" , proc_in = "cocoa_for_chocolate", proc_out = "cocoa_butter", value = 0.47 *.8, dietary_products = "Cocoa Beans and products") %>% - add_row(crop_cifos = "cocoa_beans" , proc_raw = "Cocoa beans" , proc_in = "cocoa_for_chocolate", proc_out = "cocoa_powder_cake", value = 0.53 *.8, dietary_products = "Cocoa Beans and products") %>% - add_row(crop_cifos = "cocoa_beans" , proc_raw = "Cocoa beans" , proc_in = "cocoa_for_chocolate", proc_out = "cocoa_husks", value = 0.2, dietary_products = "Cocoa Beans and products") %>% - drop_na() %>% - - # Coconut -- Source: from Technical Conversion Factors for Agricultural Commodities (http://countrystat.org/resources/documents/tcf.pdf) - # Assumptions: It adds not up to 1 due to water loss - # levels: second level where available, no data for copra only! - add_row(crop_cifos = "coconuts" , proc_raw = "coconuts" , proc_in = "coconuts_dessicatedin", proc_out = "coconuts_dessicated", value = 0.20, dietary_products = "Coconuts - Incl Copra") %>% - add_row(crop_cifos = "coconuts" , proc_raw = "coconuts" , proc_in = "coconuts_copra", proc_out = "coconuts_oil_copra", value = 0.21*0.64, dietary_products = "Coconut Oil") %>% - add_row(crop_cifos = "coconuts" , proc_raw = "coconuts" , proc_in = "coconuts_copra", proc_out = "coconuts_cake_copra", value = 0.21*0.36, dietary_products = "Coconuts - Incl Copra") %>% - - - # Cassava -- Source: from Technical Conversion Factors for Agricultural Commodities (http://countrystat.org/resources/documents/tcf.pdf); adjusted commodity tree. - # Levels: only one level in tcf doc! - add_row(crop_cifos = "cassava" , proc_raw = "cassava" , proc_in = "cassava_flourin", proc_out = "cassava_flour", value = 0.3, dietary_products = "Cassava and products") %>% - add_row(crop_cifos = "cassava" , proc_raw = "cassava" , proc_in = "cassava_tapiocain", proc_out = "cassava_tapioca", value = 0.2, dietary_products = "Cassava and products") %>% - add_row(crop_cifos = "cassava" , proc_raw = "cassava" , proc_in = "cassava_dryin", proc_out = "cassava_dry", value = 0.35, dietary_products = "Cassava and products") %>% - add_row(crop_cifos = "cassava" , proc_raw = "cassava" , proc_in = "cassava_starchin", proc_out = "cassava_starch", value = 0.25, dietary_products = "Cassava and products") %>% - - # Pearl millet -- Source: from Technical Conversion Factors for Agricultural Commodities (http://countrystat.org/resources/documents/tcf.pdf); adjusted commodity tree; fractions from "cereals/millet" - # levels: only one level - add_row(crop_cifos = "pearl_millet" , proc_raw = "pearl_millet" , proc_in = "pearl_millet_flourin", proc_out = "pearl_millet_flour", value = 0.86, dietary_products = "Millet and products") %>% - add_row(crop_cifos = "pearl_millet" , proc_raw = "pearl_millet" , proc_in = "pearl_millet_flourin", proc_out = "pearl_millet_bran", value = 0.10, dietary_products = "Millet and products") %>% - add_row(crop_cifos = "pearl_millet" , proc_raw = "pearl_millet" , proc_in = "pearl_millet_beerin", proc_out = "pearl_millet_beer", value = 4.5, dietary_products = "Millet and products") %>% - - # Oil palm -- Source: from Technical Conversion Factors for Agricultural Commodities (http://countrystat.org/resources/documents/tcf.pdf); adjusted commodity tree; fractions from "OIL PALM FRUIT" - # Assumption: oil_palm_kernels_fibre is just the difference of the sum of the listed categories to 1. - # Both kernels and fruits are always both processed - add_row(crop_cifos = "oil_palm_fruit" , proc_raw = "oil_palm_fruit" , proc_in = "oil_palm_fruitin", proc_out = "oil_palm_fruits_oil", value = 0.19, dietary_products = "Palm Oil") %>% - add_row(crop_cifos = "oil_palm_fruit" , proc_raw = "oil_palm_fruit" , proc_in = "oil_palm_fruitin", proc_out = "oil_palm_kernels_cake", value = 0.46*0.06, dietary_products = "Palm kernels") %>% - add_row(crop_cifos = "oil_palm_fruit" , proc_raw = "oil_palm_fruit" , proc_in = "oil_palm_fruitin", proc_out = "oil_palm_kernels_oil", value = 0.53*0.06, dietary_products = "Palmkernel Oil") %>% - - # plantains -- tables from countries. Assumptions: the whole plantain is to be kept whole! Question: Is that a fair assumption? - add_row(crop_cifos = "plantains" , proc_raw = "plantains" , proc_in = "plantains", proc_out = "plantains", value = 1, dietary_products = "Plantains") %>% - - # sorghum -- adjusted commodity table "Sorghum" - # levels, only level 1 used (level 2 does not make) - add_row(crop_cifos = "sorghum" , proc_raw = "sorghum" , proc_in = "sorghum_flourin", proc_out = "sorghum_flour", value = 0.9, dietary_products = "Sorghum and products") %>% - add_row(crop_cifos = "sorghum" , proc_raw = "sorghum" , proc_in = "sorghum_flourin", proc_out = "sorghum_bran", value = 0.08, dietary_products = "Sorghum and products") %>% - add_row(crop_cifos = "sorghum" , proc_raw = "sorghum" , proc_in = "sorghum_beerin", proc_out = "sorghum_beer", value = 4.85, dietary_products = "Sorghum and products") %>% #on first level - - # sugar_cane - # add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_cane" , proc_in = "sugar_cane", proc_out = "beverage_non_alc", value = 0.13*0.5, dietary_products = "Sugar cane") %>% - - # add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_cane" , proc_in = "sugar_cane", proc_out = "beverage_dist_alc", value = 0.13*0.5, dietary_products = "Sugar cane") %>% - add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_cane" , proc_in = "sugar_canein", proc_out = "sugar_raw", value = 0.11, dietary_products = "Sugar (Raw Equivalent)") %>% - add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_raw" , proc_in = "for_sugar_white", proc_out = "sugar_white", value = 0.935, dietary_products = "Sugar (Raw Equivalent)") %>% # 2nd level of sugar raw - # add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_raw" , proc_in = "for_sugar_white", proc_out = "sugar_factorylime", value = 1-0.935, dietary_products = "Sugar (Raw Equivalent)") %>% # 2nd level of sugar raw - - add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_cane" , proc_in = "sugar_canein", proc_out = "sugarcane_molasse", value = 0.05, dietary_products = "Sugar cane") %>% - add_row(crop_cifos = "sugar_cane" , proc_raw = "sugar_cane" , proc_in = "sugar_canein", proc_out = "sugarcane_bagasse", value = 0.25, dietary_products = "Sugar cane") %>% - - #sweet_potatoes -- commodity tree - add_row(crop_cifos = "sweet_potatoes" , proc_raw = "sweet_potatoes" , proc_in = "sweet_potatoe_flourin", proc_out = "sweet_potatoe_flour", value = 0.25, dietary_products = "Sweet potatoes") %>% - add_row(crop_cifos = "sweet_potatoes" , proc_raw = "sweet_potatoes" , proc_in = "sweet_potatoe_starchin", proc_out = "sweet_potatoe_starch", value = 0.2, dietary_products = "Sweet potatoes") %>% - - # tea -- - add_row(crop_cifos = "tea" , proc_raw = "tea" , proc_in = "teain", proc_out = "tea_leave", value = 1, dietary_products = "Tea (including mate)") %>% - - # yams -- - # 1 . level only (other tubers) - add_row(crop_cifos = "yams" , proc_raw = "yams" , proc_in = "yams_flourin", proc_out = "yams_flour", value = 0.25, dietary_products = "Roots, Other") %>% - add_row(crop_cifos = "yams" , proc_raw = "yams" , proc_in = "yams_dryin", proc_out = "yams_dry", value = 0.3, dietary_products = "Roots, Other") %>% - - # other_crops -- - add_row(crop_cifos = "other_crops" , proc_raw = "chilies_and_peppers_green" , proc_in = "chilies_and_peppers_green", proc_out = "chilies_and_peppers_green", value = 0.9, dietary_products = "Pulses, Other and products") %>% # Commodity tree, from Egypt "Chillies, green" - - # other_fibres -- cotton fibre fractions - add_row(crop_cifos = "other_fibres" , proc_raw = "flax_fiber_and_tow" , proc_in = "flax_fiber_and_tow", proc_out = "flax_fiber_and_tow", value = 0.33, dietary_products = "Fibres") %>% - - # other_tubers - # 1 . level only (other tubers) - add_row(crop_cifos = "other_tubers" , proc_raw = "other_tubers" , proc_in = "other_tubers_flourin", proc_out = "other_tubers_flour", value = 0.25, dietary_products = "Roots, Other") %>% - add_row(crop_cifos = "other_tubers" , proc_raw = "other_tubers" , proc_in = "other_tubers_dryin", proc_out = "other_tubers_dry", value = 0.3, dietary_products = "Roots, Other") %>% - - # cotton - cotton fibre fractions - add_row(crop_cifos = "seed_cotton" , proc_raw = "seed_cotton" , proc_in = "seed_cotton_fibresin", proc_out = "seed_cotton_fibres", value = 0.33, dietary_products = "Fibres") %>% - - # tobacco - add_row(crop_cifos = "tobacco" , proc_raw = "tobacco" , proc_in = "tobacco_leavesin", proc_out = "tobacco_leaves", value = 1, dietary_products = "Tobacco") %>% - - # Chick peas - # Levels: all pulses only one level - add_row(crop_cifos = "chick_peas" , proc_raw = "chick_peas" , proc_in = "chickpea_vegetablein", proc_out = "chickpea_vegetable", value = 0.95, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "chick_peas" , proc_raw = "chick_peas" , proc_in = "chickpea_flourin", proc_out = "chickpea_flour", value = 0.72, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "chick_peas" , proc_raw = "chick_peas" , proc_in = "chickpea_flourin", proc_out = "chickpea_bran", value = 0.23, dietary_products = "Pulses, Other and products") %>% - - # Beans dry - # Levels: all pulses only one level - add_row(crop_cifos = "beans_dry" , proc_raw = "beans_dry" , proc_in = "beans_dry_vegetable", proc_out = "beans_dry_vegetable", value = 0.95, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "beans_dry" , proc_raw = "beans_dry" , proc_in = "beans_dry_flourin", proc_out = "beans_dry_flour", value = 0.72, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "beans_dry" , proc_raw = "beans_dry" , proc_in = "beans_dry_flourin", proc_out = "beans_dry_bran", value = 0.23, dietary_products = "Pulses, Other and products") %>% - - # Other pulses - # Levels: all pulses only one level - add_row(crop_cifos = "other_pulses" , proc_raw = "other_pulses" , proc_in = "other_pulses_vegetable", proc_out = "other_pulses_vegetable", value = 0.95, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "other_pulses" , proc_raw = "other_pulses" , proc_in = "other_pulses_flourin", proc_out = "other_pulses_flour", value = 0.72, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "other_pulses" , proc_raw = "other_pulses" , proc_in = "other_pulses_flourin", proc_out = "other_pulses_bran", value = 0.23, dietary_products = "Pulses, Other and products") %>% - - # lentils - # Levels: all pulses only one level - add_row(crop_cifos = "lentils" , proc_raw = "lentils" , proc_in = "lentils_vegetable", proc_out = "lentils_vegetable", value = 0.95, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "lentils" , proc_raw = "lentils" , proc_in = "lentils_flourin", proc_out = "lentils_flour", value = 0.72, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "lentils" , proc_raw = "lentils" , proc_in = "lentils_flourin", proc_out = "lentils_bran", value = 0.23, dietary_products = "Pulses, Other and products") %>% - - # pigeon_peas - # Levels: all pulses only one level - add_row(crop_cifos = "pigeon_peas" , proc_raw = "pigeon_peas" , proc_in = "pigeon_peas_vegetable", proc_out = "pigeon_peas_vegetable", value = 0.95, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "pigeon_peas" , proc_raw = "pigeon_peas" , proc_in = "pigeon_peas_flourin", proc_out = "pigeon_peas_flour", value = 0.72, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "pigeon_peas" , proc_raw = "pigeon_peas" , proc_in = "pigeon_peas_flourin", proc_out = "pigeon_peas_bran", value = 0.23, dietary_products = "Pulses, Other and products") %>% - - # cow_peas_dry - # Levels: all pulses only one level - add_row(crop_cifos = "cow_peas_dry" , proc_raw = "cow_peas_dry" , proc_in = "cow_peas_dry_vegetable", proc_out = "cow_peas_dry_vegetable", value = 0.95, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "cow_peas_dry" , proc_raw = "cow_peas_dry" , proc_in = "cow_peas_dry_flourin", proc_out = "cow_peas_dry_flour", value = 0.72, dietary_products = "Pulses, Other and products") %>% - add_row(crop_cifos = "cow_peas_dry" , proc_raw = "cow_peas_dry" , proc_in = "cow_peas_dry_flourin", proc_out = "cow_peas_dry_bran", value = 0.23, dietary_products = "Pulses, Other and products") %>% - - # groundnuts - commodity tree - #1 level - add_row(crop_cifos = "groundnuts" , proc_raw = "groundnuts_in_shell" , proc_in = "groundnuts_shellin", proc_out = "groundnut_shelled", value = 0.7, dietary_products = "Groundnuts (Shelled Eq)") %>% - add_row(crop_cifos = "groundnuts" , proc_raw = "groundnuts_in_shell" , proc_in = "groundnuts_shellin", proc_out = "groundnut_shell", value = 0.3, dietary_products = "Groundnuts (Shelled Eq)") %>% - # 2nd level - add_row(crop_cifos = "groundnuts" , proc_raw = "groundnut_shelled" , proc_in = "groundnuts_oilin", proc_out = "groundnut_oil", value = 0.43, dietary_products = "Groundnut Oil") %>% - add_row(crop_cifos = "groundnuts" , proc_raw = "groundnut_shelled" , proc_in = "groundnuts_oilin", proc_out = "groundnut_cake", value = 0.54, dietary_products = "Groundnut Oil") %>% - - add_row(crop_cifos = "groundnuts" , proc_raw = "groundnut_shelled" , proc_in = "groundnuts_butterin", proc_out = "groundnut_butter", value = 0.85, dietary_products = "Groundnuts (Shelled Eq)") %>% - - # Other cereals - commodity tree - add_row(crop_cifos = "other_cereals" , proc_raw = "other_cereals" , proc_in = "other_cereals_flourin", proc_out = "other_cereals_flour", value = 0.8, dietary_products = "Cereals, Other") %>% - add_row(crop_cifos = "other_cereals" , proc_raw = "other_cereals" , proc_in = "other_cereals_flourin", proc_out = "other_cereals_bran", value = 0.2, dietary_products = "Cereals, Other") %>% - add_row(crop_cifos = "other_cereals" , proc_raw = "other_cereals" , proc_in = "other_cereals_wholein", proc_out = "other_cereals_whole", value = 1, dietary_products = "Cereals, Other") %>% - - # other_oil_crops - commodity tree (tables) - # 1 level - add_row(crop_cifos = "other_oil_crops" , proc_raw = "olives" , proc_in = "olive_tablein", proc_out = "olive_table", value = 1, dietary_products = "Olives (including preserved)") %>% - add_row(crop_cifos = "other_oil_crops" , proc_raw = "olives" , proc_in = "olive_oilin", proc_out = "olive_oil", value = 0.25, dietary_products = "Olive Oil") %>% #virgine and residue oil mixed! Therfore 5% more oil - add_row(crop_cifos = "other_oil_crops" , proc_raw = "olives" , proc_in = "olive_oilin", proc_out = "olive_cake", value = 0.35, dietary_products = "Olives (including preserved)") %>% - - # temperate_fruit - assumption: Whole fruit is used and no losses occur - add_row(crop_cifos = "temperate_fruit" , proc_raw = "temperate_fruit" , proc_in = "temperate_fruit_whole", proc_out = "temperate_fruit_whole", value =mean(c(0.5, 0.35,0.29, 0.25)) , dietary_products = "Apples and products") %>% #tcf fao, fractions from apples, plums, apricots, raisins - add_row(crop_cifos = "temperate_fruit" , proc_raw = "temperate_fruit" , proc_in = "temperate_fruit_juice", proc_out = "temperate_fruit_juice", value = mean(c(0.22, 0.75)) , dietary_products = "Apples and products") %>% #tcf fao, taken from apple, raisins - add_row(crop_cifos = "temperate_fruit" , proc_raw = "temperate_fruit" , proc_in = "temperate_fruit_juice", proc_out = "temperate_fruit_pulp", value = mean(c(0.4)) , dietary_products = "Apples and products") %>% #not in fao. taken from apple - - # tropical_fruits - assumption: Whole fruit is used and no losses occur - add_row(crop_cifos = "tropical_fruits" , proc_raw = "tropical_fruits" , proc_in = "tropical_fruits_whole", proc_out = "tropical_fruits_whole", value = mean(c(0.65,0.52)), dietary_products = "Oranges, Mandarines") %>% #tcf fao(canned), orange canned, pineapples - add_row(crop_cifos = "tropical_fruits" , proc_raw = "tropical_fruits" , proc_in = "tropical_fruits_juice", proc_out = "tropical_fruits_juice", value = mean(c(0.1,0.1,0.2)), dietary_products = "Oranges, Mandarines") %>%#tcf fao, mean of oranges, mango, pineapple - add_row(crop_cifos = "tropical_fruits" , proc_raw = "tropical_fruits" , proc_in = "tropical_fruits_juice", proc_out = "tropical_fruits_pulp", value = mean(c(0.4,0.55)), dietary_products = "Oranges, Mandarines") %>%#tcf fao, mean of oranges, mango - - #Adding more details to existing crops: - # other_vegetables - add_row(crop_cifos = "other_vegetables" , proc_raw = "cabbages" , proc_in = "cabbage_whitein", proc_out = "cabbage_white", value = 0.65, dietary_products = "Vegetables, Other") %>% - - # red_vegetables - add_row(crop_cifos = "red_vegetables" , proc_raw = "carrots" , proc_in = "carrot_winterin", proc_out = "carrot_winter", value = 0.65, dietary_products = "Vegetables, Other") %>% - - # green_vegetables - add_row(crop_cifos = "green_vegetables" , proc_raw = "spinach" , proc_in = "spinachin", proc_out = "spinach", value = 0.65, dietary_products = "Vegetables, Other") %>% - - # Maize - commodity tree - # 2 levels - add_row(crop_cifos = "maize" , proc_raw = "maize_flour" , proc_in = "maize_flourin", proc_out = "maize_flour", value = 0.82, dietary_products = "Maize and products") %>% - add_row(crop_cifos = "maize" , proc_raw = "maize_flour" , proc_in = "maize_flourin", proc_out = "maize_bran", value = 0.11, dietary_products = "Maize and products") %>% #for feed - add_row(crop_cifos = "maize" , proc_raw = "maize_flour" , proc_in = "maize_flourin", proc_out = "maize_germ", value = 0.06, dietary_products = "Maize and products") %>% - add_row(crop_cifos = "maize" , proc_raw = "maize_germ" , proc_in = "maize_oilin", proc_out = "maize_cake", value = 0.52, dietary_products = "Maize and products") %>% - add_row(crop_cifos = "maize" , proc_raw = "maize_germ" , proc_in = "maize_oilin", proc_out = "maize_oil", value = 0.45, dietary_products = "Maize Germ Oil") %>% - - # Grass crops - ## grass_arable - Prefereably this should represent a grass type like lollium ssp. - add_row(crop_cifos = "grass_arable" , proc_raw = "grass_arable" , proc_in = "grass_arable_dry", proc_out = "grass_arable_hay", value = 0.88, dietary_products = "Roughage") %>% # Timothy (Phleum pratense), hay (feedepedia) - add_row(crop_cifos = "grass_arable" , proc_raw = "grass_arable" , proc_in = "grass_arable_fresh", proc_out = "grass_arable_fresh", value = (1-0.86), dietary_products = "Forage and silage, grasses nes") %>% # Source: https://doi.org/10.1111/j.1469-8137.1994.tb04036.x - I take the upper bound - add_row(crop_cifos = "grass_arable" , proc_raw = "grass_arable" , proc_in = "grass_arable_silage", proc_out = "grass_arable_silage", value = 0.308, dietary_products = "Roughage") %>% #, silage (feedepedia) - - ## grass_pasture - Source: https://www.feedipedia.org/node/16886; Surrogate specie: Timothy grass (Phleum pratense) - add_row(crop_cifos = "grass_pasture" , proc_raw = "grass_pasture" , proc_in = "grass_pasture_dry", proc_out = "grass_pasture_hay", value = 0.88, dietary_products = "Roughage") %>% #Timothy (Phleum pratense), hay (feedepedia) - add_row(crop_cifos = "grass_pasture" , proc_raw = "grass_pasture" , proc_in = "grass_pasture_fresh", proc_out = "grass_pasture_fresh", value = 0.27, dietary_products = "Forage and silage, grasses nes") %>% # Timothy (Phleum pratense), aerial part, fresh, (feedepedia) - add_row(crop_cifos = "grass_pasture" , proc_raw = "grass_pasture" , proc_in = "grass_pasture_silage", proc_out = "grass_pasture_silage", value = 0.308, dietary_products = "Roughage") %>% #Timothy grass (Phleum pratense) silage, (feedepedia) - - ## grass_rangeland - - add_row(crop_cifos = "grass_rangeland" , proc_raw = "grass_rangeland" , proc_in = "grass_rangeland_fresh", proc_out = "grass_rangeland_fresh", value = 0.30, dietary_products = "Forage and silage, grasses nes") %>% # Source: https://doi.org/10.1111/j.1469-8137.1994.tb04036.x - I take the lower bound - - ## alfalfafor - # grass_pasture - Source: Source: https://www.feedipedia.org/node/275 - add_row(crop_cifos = "alfalfafor" , proc_raw = "alfalfafor" , proc_in = "alfalfafor_dry", proc_out = "alfalfafor_hay", value = 0.894, dietary_products = "Forage and silage, legumes") %>% #Alfalfa, hay, (feedepedia) - add_row(crop_cifos = "alfalfafor" , proc_raw = "alfalfafor" , proc_in = "alfalfafor_fresh", proc_out = "alfalfafor_fresh", value = 0.199, dietary_products = "Forage and silage, legumes") %>% #Alfalfa fresh (feedepedia) - add_row(crop_cifos = "alfalfafor" , proc_raw = "alfalfafor" , proc_in = "alfalfafor_silage", proc_out = "alfalfafor_silage", value = 0.308, dietary_products = "Forage and silage, legumes") %>% #Alfalfa, silage (feedepedia) - - # cloverfor - Source: https://www.feedipedia.org/node/246 - add_row(crop_cifos = "cloverfor" , proc_raw = "cloverfor" , proc_in = "cloverfor_dry", proc_out = "cloverfor_hay", value = 0.894, dietary_products = "Roughage") %>% # Alfalfa, hay, (feedepedia) - add_row(crop_cifos = "cloverfor" , proc_raw = "cloverfor" , proc_in = "cloverfor_fresh", proc_out = "cloverfor_fresh", value = 0.19, dietary_products = "Forage and silage, legumes") %>% #Red clover (Trifolium pratense), aerial part, fresh (feedepedia) - add_row(crop_cifos = "cloverfor" , proc_raw = "cloverfor" , proc_in = "cloverfor_silage", proc_out = "cloverfor_silage", value = 0.277, dietary_products = "Roughage") %>% #Red clover (Trifolium pratense), silage (feedepedia) - - # ryefor - Source: https://www.feedipedia.org/node/385 - # According to FAO this might relate to fast growing grass which is covered by grass already -- https://www.fao.org/es/faodef/fdef11e.htm -- RYE GRASS FOR FORAGE Italian ryegrass (Lolium multiflorum); English, perennial ryegrass (L. perenne) (feedepedia) - add_row(crop_cifos = "ryefor" , proc_raw = "ryefor" , proc_in = "ryefor_freshin", proc_out = "ryefor_fresh", value = 0.16, dietary_products = "Forage and silage, legumes") %>% # Reference: Feedipedia: Rye, aerial part, fresh (feedepedia) - add_row(crop_cifos = "ryefor" , proc_raw = "ryefor" , proc_in = "ryefor_dryin", proc_out = "ryefor_hay", value = 0.92, dietary_products = "Forage and silage, legumes") %>% # Reference: Feedipedia: Rye straw (feedepedia) - add_row(crop_cifos = "ryefor" , proc_raw = "ryefor" , proc_in = "ryefor_silage", proc_out = "ryefor_silage", value = 0.277, dietary_products = "Forage and silage, legumes") %>% # #Red clover (Trifolium pratense), silage (feedepedia) - - # fornesfor - add_row(crop_cifos = "fornesfor" , proc_raw = "fornesfor_feed" , proc_in = "fornesfor_feedin", proc_out = "fornesfor_plant", value = 0.65, dietary_products = "Forage products") %>% #comes from cabbage - - # beetfor - Source: https://www.feedipedia.org/node/534 - add_row(crop_cifos = "beetfor" , proc_raw = "beetfor_feed" , proc_in = "beetfor_feedin", proc_out = "beetfor_leaves", value = 0.179, dietary_products = "Forage products") %>% # Beet leaves, fresh - add_row(crop_cifos = "beetfor" , proc_raw = "beetfor_feed" , proc_in = "beetfor_feedin", proc_out = "beetfor_root", value = 0.161, dietary_products = "Forage products") %>% # Beet root, fodder type, fresh - - # cabbagefor - add_row(crop_cifos = "cabbagefor" , proc_raw = "cabbagefor_feed" , proc_in = "cabbagefor_feedin", proc_out = "cabbagefor_plant", value = 0.65, dietary_products = "Forage products") %>% - - # carrotfor - Source: https://www.feedipedia.org/node/539 - add_row(crop_cifos = "carrotfor" , proc_raw = "carrotfor_feed" , proc_in = "carrotfor_feedin", proc_out = "carrots_fresh", value = 0.107 , dietary_products = "Forage products") %>% # Beet leaves, fresh - add_row(crop_cifos = "carrotfor" , proc_raw = "carrotfor_feed" , proc_in = "carrotfor_feedin", proc_out = "carrots_tops_fresh", value = 0.164, dietary_products = "Forage products") %>% # Beet root, fodder type, fresh - - # legumenesfor - Source: https://www.feedipedia.org/node/4926 --> Faba Bean taken as a proxy - # Levels: all pulses only one level - add_row(crop_cifos = "legumenesfor" , proc_raw = "legumenesfor_feed" , proc_in = "legumenesfor_feedin", proc_out = "legumenesfor_bean", value = 0.866 , dietary_products = "Forage and silage, legumes") %>% # Faba bean (Vicia faba), all cultivars - add_row(crop_cifos = "legumenesfor" , proc_raw = "legumenesfor_feed" , proc_in = "legumenesfor_feedin", proc_out = "legumenesfor_straw", value = 0.194 , dietary_products = "Forage and silage, legumes") %>% # Faba bean (Vicia faba), aerial part, fresh - - # maizefor - https://www.feedipedia.org/node/13883 AND commodity tree FAO - add_row(crop_cifos = "maizefor" , proc_raw = "maize_whole_feed" , proc_in = "maize_silagein", proc_out = "maize_silage", value = 0.325, dietary_products = "Forage products") %>% # Reference: Maize silage, dry matter 30-35% from https://www.feedipedia.org/node/12871 - add_row(crop_cifos = "maizefor" , proc_raw = "maize_whole_feed" , proc_in = "maize_oilin", proc_out = "maize_oil", value = 0.45*0.06, dietary_products = "Forage products") %>% # Reference: Commodidty tree fao same as with humans - add_row(crop_cifos = "maizefor" , proc_raw = "maize_whole_feed" , proc_in = "maize_oil", proc_out = "maize_cake", value = 0.52*0.06, dietary_products = "Forage products") %>% # Reference: Commodidty tree fao same as with humans - - # oilseedfor - from Commodity tree (Soyabeans taken as a proxy) - add_row(crop_cifos = "oilseedfor" , proc_raw = "oilseedfor" , proc_in = "oilseedfor_for_oil", proc_out = "oilseedfor_for_oil", value = 0.19, dietary_products = "Forage products") %>% # Reference: Soyabean tcf - add_row(crop_cifos = "oilseedfor" , proc_raw = "oilseedfor" , proc_in = "oilseedfor_for_oil", proc_out = "oilseedfor_cake", value = 0.71, dietary_products = "Forage products") %>% # Reference:Soyabean tcf - add_row(crop_cifos = "oilseedfor" , proc_raw = "oilseedfor" , proc_in = "oilseedfor_for_oil", proc_out = "oilseedfor_hulls", value = 0.07, dietary_products = "Forage products") %>% # Reference: Soyabean tcf - - - # swedefor - https://www.feedipedia.org/node/534 - add_row(crop_cifos = "swedefor" , proc_raw = "swedefor_feed" , proc_in = "swedefor_feedin", proc_out = "swedefor_leave", value = 0.179, dietary_products = "Forage products") %>% # Beet leaves, fresh - add_row(crop_cifos = "swedefor" , proc_raw = "swedefor_feed" , proc_in = "swedefor_feedin", proc_out = "swedefor_root", value = 0.161, dietary_products = "Forage products") %>% # Beet root, fodder type, fresh - - # turnipfor - https://www.feedipedia.org/node/534 - add_row(crop_cifos = "turnipfor" , proc_raw = "turnipfor_feed" , proc_in = "turnipfor_feedin", proc_out = "turnipfor_leave", value = 0.179, dietary_products = "Forage products") %>% # Beet leaves, fresh - add_row(crop_cifos = "turnipfor" , proc_raw = "turnipfor_feed" , proc_in = "turnipfor_feedin", proc_out = "turnipfor_root", value = 0.161, dietary_products = "Forage products") %>% # Beet root, fodder type, fresh - - # vegfor - add_row(crop_cifos = "vegfor" , proc_raw = "vegfor_feed" , proc_in = "vegfor_feedin", proc_out = "vegfor_whole", value = 0.65, dietary_products = "Forage products") %>% - - # Here I change the existing processing products that are not according to FAO tcf document - # Barley from fao tcf document - add_row(crop_cifos = "barley" , proc_raw = "barley" , proc_in = "barley_for_pearlin", proc_out = "barley_pearl", value = 0.72*0.55, dietary_products = "Barley and products") %>% # - add_row(crop_cifos = "barley" , proc_raw = "barley" , proc_in = "barley_for_pearlin", proc_out = "barley_flour", value = 0.72*.45, dietary_products = "Barley and products") %>% # - add_row(crop_cifos = "barley" , proc_raw = "barley" , proc_in = "barley_for_pearlin", proc_out = "barley_bran", value = 0.19, dietary_products = "Barley and products") %>% # - add_row(crop_cifos = "barley" , proc_raw = "barley" , proc_in = "barley_for_pearlin", proc_out = "barley_hulls", value = 0.07, dietary_products = "Barley and products") %>% # - - # add_row(crop_cifos = "barley" , proc_raw = "barley" , proc_in = "barley_for_malt", proc_out = "barley_maltculms", value = 0.24, dietary_products = "Barley and products") %>% # - add_row(crop_cifos = "barley" , proc_raw = "barley" , proc_in = "barley_for_malt", proc_out = "barley_malt", value = 0.73, dietary_products = "Barley and products") %>% # - # add_row(crop_cifos = "barley" , proc_raw = "barley_malt" , proc_in = "for_beer", proc_out = "brewers_grain", value = 8, dietary_products = "Barley and products") %>% # - add_row(crop_cifos = "barley" , proc_raw = "barley_malt" , proc_in = "for_beer", proc_out = "barley_beer", value = 8*0.8, dietary_products = "Barley and products") %>% # - - # Barley for feed - add_row(crop_cifos = "barley" , proc_raw = "barley_malt" , proc_in = "barley_feed", proc_out = "grain_barley", value = 0.98, dietary_products = "Barley and products") %>% # - - #Wheat- tcf table - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_for_wholemeal", proc_out = "wheat_wholemeal", value = 0.85, dietary_products = "Wheat and products") %>% - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_for_wholemeal", proc_out = "wheat_wholemeal_germ", value = 0.01, dietary_products = "Wheat and products") %>% - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_for_wholemeal", proc_out = "wheat_feedmeal", value = 0.02, dietary_products = "Wheat and products") %>% # - - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_flour", proc_out = "wheat_germ", value = 0.02, dietary_products = "Wheat and products") %>% #ref: slightly adjusted from tcf FAO so that it adds up to 88% DM - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_flour", proc_out = "wheat_flour", value = 0.75, dietary_products = "Wheat and products") %>% - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_flour", proc_out = "wheat_branh", value = 0.11, dietary_products = "Wheat and products") %>% - - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_feed", proc_out = "grain_wheat", value = 0.88, dietary_products = "Wheat and products") %>% #wheat harvested ca 12 %DM - - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_flourba", proc_out = "wheat_flour", value = 0.73, dietary_products = "Wheat and products") %>% - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_flourba", proc_out = "wheat_feedmeal", value = 0.02, dietary_products = "Wheat and products") %>% - add_row(crop_cifos = "wheat" , proc_raw = "wheat" , proc_in = "wheat_flourba", proc_out = "wheat_brana", value = 0.12, dietary_products = "Wheat and products") %>% # - - # Rapeseed- tcf table - add_row(crop_cifos = "rapeseed" , proc_raw = "rapeseed" , proc_in = "rapeseed_oil", proc_out = "rapeseed_oil", value = 0.38, dietary_products = "Rape and Mustard Oil") %>% - add_row(crop_cifos = "rapeseed" , proc_raw = "rapeseed" , proc_in = "rapeseed_oil", proc_out = "rapeseed_cake", value = 0.6, dietary_products = "Rape and Mustardseed") %>% - - # potatoes - tcf table - add_row(crop_cifos = "potatoes" , proc_raw = "potatoes" , proc_in = "potatoes_whole", proc_out = "potatoes_dry", value = 0.17, dietary_products = "Potatoes and products") %>% - add_row(crop_cifos = "potatoes" , proc_raw = "potatoes" , proc_in = "potatoes_whole", proc_out = "tuber_peel", value = 0.02, dietary_products = "Potatoes and products") %>% - - add_row(crop_cifos = "potatoes" , proc_raw = "potatoes" , proc_in = "potatoes_flour", proc_out = "potatoes_flour", value = 0.2, dietary_products = "Potatoes and products") %>% - add_row(crop_cifos = "potatoes" , proc_raw = "potatoes" , proc_in = "potatoes_flour", proc_out = "tuber_peel", value = 0.02, dietary_products = "Potatoes and products") %>% - - add_row(crop_cifos = "potatoes" , proc_raw = "potatoes" , proc_in = "potatoes_starch", proc_out = "potatoes_starch", value = 0.19, dietary_products = "Potatoes and products") %>% - add_row(crop_cifos = "potatoes" , proc_raw = "potatoes" , proc_in = "potatoes_starch", proc_out = "tuber_peel", value = 0.02, dietary_products = "Potatoes and products") %>% - - # Soybeans - tcf table if not otherwise stated - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans" , proc_in = "soyabean_for_oil", proc_out = "soyabean_oil", value = 0.19, dietary_products = "Soyabean Oil") %>% - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans" , proc_in = "soyabean_for_oil", proc_out = "soyabean_cake", value = 0.71, dietary_products = "Soyabeans") %>% - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans" , proc_in = "soyabean_for_oil", proc_out = "soyabean_hulls", value = 0.07, dietary_products = "Soyabeans") %>% - - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans" , proc_in = "soyabean_for_table", proc_out = "soyabean_table", value = 0.71, dietary_products = "Soyabeans") %>% #ref: Ollie - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans" , proc_in = "soyabean_for_table", proc_out = "soyabean_hulls", value = 0.07, dietary_products = "Soyabeans") %>% #ref: Ollie - - # feed - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans_feed" , proc_in = "soyabean_for_feed", proc_out = "soyabean_feed_oil", value = 0.19, dietary_products = "Soyabean Oil") %>% - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans_feed" , proc_in = "soyabean_for_feed", proc_out = "soyabean_feed_cake", value = 0.71, dietary_products = "Soyabeans") %>% - add_row(crop_cifos = "soybeans" , proc_raw = "soybeans_feed" , proc_in = "soyabean_for_feed", proc_out = "soyabean_feed_hulls", value = 0.07, dietary_products = "Soyabeans") %>% - - # sunflower - tcf fao - add_row(crop_cifos = "sunflower" , proc_raw = "sunflower_seed" , proc_in = "sunflower_seed", proc_out = "sunflower_oil", value = 0.41, dietary_products = "Soyabean Oil") %>% - add_row(crop_cifos = "sunflower" , proc_raw = "sunflower_seed" , proc_in = "sunflower_for_oil", proc_out = "sunflower_cake", value = 0.47, dietary_products = "Soyabeans") %>% - add_row(crop_cifos = "sunflower" , proc_raw = "sunflower_seed" , proc_in = "sunflower_for_oil", proc_out = "sunflower_hulls", value = 0.07, dietary_products = "Soyabeans") %>% #ref from ollie (old proccessing sheet) - - # Paddy Rice - tcf fao - # husked rice - add_row(crop_cifos = "rice" , proc_raw = "rice" , proc_in = "rice_husk", proc_out = "rice_husked", value = 0.77, dietary_products = "Rice (Milled Equivalent)") %>% - add_row(crop_cifos = "rice" , proc_raw = "rice" , proc_in = "rice_husk", proc_out = "rice_hulls", value = 0.25, dietary_products = "Rice (Milled Equivalent)") %>% - # 2nd stage - add_row(crop_cifos = "rice" , proc_raw = "rice_husked" , proc_in = "rice_husked_milled", proc_out = "rice_husked_milled", value = 0.9, dietary_products = "Rice (Milled Equivalent)") %>% - add_row(crop_cifos = "rice" , proc_raw = "rice_husked" , proc_in = "rice_husked_milled", proc_out = "rice_bran", value = 0.15, dietary_products = "Rice (Milled Equivalent)") %>% - - # Milled paddy rice - add_row(crop_cifos = "rice" , proc_raw = "rice" , proc_in = "rice_paddy_milled", proc_out = "rice_paddy_milled", value = 0.67, dietary_products = "Rice (Milled Equivalent)") %>% - add_row(crop_cifos = "rice" , proc_raw = "rice" , proc_in = "rice_paddy_milled", proc_out = "hulls_paddy_milled", value = 0.25, dietary_products = "Rice (Milled Equivalent)") %>% - add_row(crop_cifos = "rice" , proc_raw = "rice" , proc_in = "rice_paddy_milled", proc_out = "bran_paddy_milled", value = 0.8, dietary_products = "Rice (Milled Equivalent)") %>% - - # 2nd stage for bran oil/cake - add_row(crop_cifos = "rice" , proc_raw = "bran_paddy_milled" , proc_in = "rice_bran_oil", proc_out = "rice_bran_oil", value = 0.80, dietary_products = "Ricebran Oil") %>% - add_row(crop_cifos = "rice" , proc_raw = "bran_paddy_milled" , proc_in = "rice_bran_oil", proc_out = "rice_bran_cake", value = 0.14, dietary_products = "Ricebran Oil") %>% - - # coffee - feedepedia -> https://www.feedipedia.org/node/549 - add_row(crop_cifos = "arabica_coffee" , proc_raw = "coffee_green" , proc_in = "coffee_green", proc_out = "coffee_roasted", value = 0.8, dietary_products = "Coffee and products") %>% - add_row(crop_cifos = "arabica_coffee" , proc_raw = "coffee_green" , proc_in = "coffee_green", proc_out = "coffee_hulls", value = 0.2, dietary_products = "Cocoa Beans and products") %>%#fraction comes from the cocoa husk. Could be improbved - - # Sesame - add_row(crop_cifos = "sesame" , proc_raw = "sesame" , proc_in = "sesame_seed", proc_out = "sesame_oil", value = 0.42, dietary_products = "Sesameseed Oil") %>% #ref from old processing sheet - add_row(crop_cifos = "sesame" , proc_raw = "sesame" , proc_in = "sesame_seed", proc_out = "sesame_cake", value = 0.57, dietary_products = "Sesame seed") %>% - - # Banana - add_row(crop_cifos = "bananas" , proc_raw = "bananas" , proc_in = "bananas", proc_out = "bananas", value = 1, dietary_products = "Bananas") %>% #ref from old processing sheet - - # Sugar beets - tcf fao - add_row(crop_cifos = "sugar_beet" , proc_raw = "sugar_beet" , proc_in = "for_sugar_refined", proc_out = "sugarbeet_toptails", value = 0.1, dietary_products = "Sugar beet") %>% #no reference - add_row(crop_cifos = "sugar_beet" , proc_raw = "sugar_beet" , proc_in = "for_sugar_refined", proc_out = "sugarbeet_pulp", value = 0.07, dietary_products = "Sugar beet") %>% - add_row(crop_cifos = "sugar_beet" , proc_raw = "sugar_beet" , proc_in = "for_sugar_refined", proc_out = "sugarbeet_molasses", value = 0.04, dietary_products = "Sugar beet") %>% - add_row(crop_cifos = "sugar_beet" , proc_raw = "sugar_beet" , proc_in = "for_sugar_refined", proc_out = "sugar_refined", value = 0.935*0.14, dietary_products = "Sugar (Raw Equivalent)") %>% - - # Treenuts - add_row(crop_cifos = "treenuts" , proc_raw = "walnuts" , proc_in = "walnuts", proc_out = "walnuts", value = 0.04, dietary_products = "Nuts and products") %>% - add_row(crop_cifos = "treenuts" , proc_raw = "walnuts" , proc_in = "walnuts", proc_out = "nut_shell", value = 0.04, dietary_products = "Nuts and products") - - -# Pulling crops that are updated -vec_proc_new = proc_new %>% distinct_at(vars(crop_cifos)) %>% pull(crop_cifos) - -# # Anti joim -# dat_proc_old = dat_complete %>% anti_join(., vec_proc_new) -# -# Creating the final processing sheet -dat_proc_new = dat_complete %>% - dplyr::filter(!crop_cifos %in% vec_proc_new, - !proc_out == "sugarbeet_toptails") %>% - bind_rows(.,proc_new) %>% - mutate(dietary_products = ifelse(proc_out == "Sugar_raw" & proc_raw == "Sugar beets" | - proc_out == "sugar_raw" & proc_raw == "sugar_cane", - "Sugar (Raw Equivalent)", dietary_products)) %>% - dplyr::mutate(across(c("crop_cifos":"proc_out"), tolower)) %>% - dplyr::mutate(proc_raw = gsub("\\,", "", proc_raw), - proc_raw = gsub(" ", "_", proc_raw), - proc_raw = case_when( - proc_raw %in% (dat_proc_new %>% pull(proc_out)) & - proc_raw != proc_out~ proc_raw, - TRUE ~ crop_cifos)) - -dat_proc_new %>% write_csv("Input_data/dat_proc_new.csv") -# dat = read_csv("Input_data/dat_proc_new.csv") - -# Adding the animal part to the processing - -# Downloading the FAO stat Food balance sheet -# FoodBalanceSheetFAO = get_faostat_bulk("FBS", "Input_data") #downloads the data bulk from faostat - -# FoodBalanceSheetFAO - -dat_proc_asf = - Processing_sheet %>% dplyr::select(-c(6,7)) %>% - clean_names() %>% rename(proc_raw = pro_raw) %>% - slice(269:363) %>% - dplyr::mutate(dietary_products= case_when(proc_raw == "Milk" ~ "Milk - Excluding Butter", - TRUE ~ dietary_products), - dietary_products= case_when(proc_out == "Butter" ~ "Butter, Ghee", - TRUE ~ dietary_products), - dietary_products= case_when(dietary_products == "Fish (Calculated CiFoS)" ~ "Fish, Seafood", - TRUE ~ dietary_products), - proc_out = case_when(proc_out == "Butter_Milk" ~ "Butter_milk", - TRUE ~ proc_out)) #this was written with a uppercase here and a lower case in the food losses sheet - - - -Processing_sheet_final = bind_rows(dat_proc_new %>% dplyr::select(-crop_cifos),dat_proc_asf) -write_csv(Processing_sheet_final, "Input_data/processing_sheet.csv") - - -# Human nutrition sheet --------------------------------------------------- -# Human nutrtion sheet -dat_humnutr = read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Human_Nutr") - -# Checking what is missing in the humann nutrition shheet so that we can add what is missing from the RIVM sheet -dat_humnutr_cifos = dat_humnutr %>% janitor::row_to_names(2) %>% janitor::clean_names() %>% - dplyr::filter(!family %in% c("Dairy", "Fish", "Meat", "Egg")) %>% mutate(product_lc = tolower(product)) -# write_csv(dat_humnutr_cifos, "Input_data/dat_humnutr_cifos.csv") - -dat_humnutr_cifos %>% - dplyr::select(c(4:5), product_lc) %>% - right_join(., (dat_proc_newdat_humnutr_cifos), - by = c("product_lc"="proc_out")) %>% - dplyr::filter(is.na(product)) %>% - dplyr::anti_join(., dat_procout_demi, by = c("product_lc"="product")) %>% - dplyr::anti_join(.,dat_map_old_new_procout,by = c("product"="new_procout")) - -# write_csv(proc_out_new, "Input_data/proc_out_new.csv") - -# Checking the overlap of updated processing sheet with demis nutritional sheet -dat_procout_demi = read_csv("Input_data/demi_human_food.csv") %>% - filter(!product=="g" & - !product =="barley_maltculms") %>% - dplyr::select(c(4:5), product) - -dat_procout_to_find_demi = dat_procout_all %>% dplyr::filter(is.na(original_id)) %>%# this needs to be checked for in the old human nutrition sheet - write_csv("Input_data/humNutr_demitofind.csv") - - -# Manual Mapping Human Nutrition ------------------------------------------ -# Careful -- Manual mapping -- Which procout is in the old human nutrition sheet that was not detected by the join above. Also mapping for non-human food procouts -dat_map_old_new_procout = - read_csv("Input_data/map_old_new_procout_2.csv") %>% - dplyr::filter(food_cat == "food" & is.na(old_procout)) - - -# These prcouts are neither in the old processing sheet nor in demis sheet -proc_out_missing = - dat_proc_new %>% dplyr::select(proc_out, dietary_products) %>% - dplyr::left_join(., dat_humnutr_cifos %>% select(product_lc, product) , by = c("proc_out" ="product_lc")) %>% - dplyr::left_join(.,dat_map_old_new_procout,by = c("proc_out"="new_procout")) %>% - dplyr::left_join(., dat_procout_demi, by = c("proc_out"="product")) %>% - dplyr::filter(is.na(original_id)) %>% - # dplyr::filter(!is.na(dietary_products)) %>% - dplyr::filter(food_cat == "food") %>% - distinct_at(vars(proc_out)) - - - -# new procout added from demi's sheet -# Food waste crops -food_psf_new = read_csv("Input_data/food_sheet_demi_distinct.csv") %>% - slice(-1) %>% - dplyr::select(-c(2,3,4,41)) %>% - rename(na_2 = `na...19`, - na = `na...5`) %>% - type.convert() %>% - mutate(epa = as.numeric(epa), - dha = as.numeric(dha ), - cholestrol = as.numeric(cholestrol ), - i =as.numeric(i), - a = as.numeric(a) , - b12 = as.numeric(b12), - d = as.numeric(d), - e = as.numeric(e), - vit_k = as.numeric(vit_k)) %>% - drop_na(product) %>% - filter(!product == "groundnut_shelled") %>% - select(-raw_cooked , -na) - -vec_food_new = food_psf_new %>% pull(product) - - -# These are the procouts from the old sheet that we matched the new procouts -food_old_match = - read_csv("Input_data/map_old_new_procout_2.csv") %>% - dplyr::filter(food_cat == "food" & !is.na(old_procout)) %>% - left_join(dat_humnutr_cifos, by = c("old_procout"="product"))%>% - type.convert() %>% - group_by(new_procout) %>% - summarise_if(is.numeric, mean, na.rm=T ) %>% - mutate(raw_nevo = "agg", - cooked_nevo = "agg", - na = "Food", - family = "NA", - product = new_procout) %>% - relocate(product, .before = "weight") %>% - relocate(family, .before = "product") %>% - relocate( na, .before = "family") %>% - relocate(cooked_nevo, .before = "na") %>% - relocate(raw_nevo, .before = "cooked_nevo") %>% - select(-new_procout,-family , -na,-raw_nevo ,-cooked_nevo) %>% - filter(!product %in% vec_food_new) - - -# procout asf -food_asf = dat_humnutr %>% janitor::row_to_names(2) %>% janitor::clean_names() %>% - dplyr::filter(family %in% c("Dairy", "Fish", "Meat", "Egg")) %>% - mutate(product_lc = tolower(product)) %>% - type.convert() %>% - select(-product_lc,-family , -na, -cooked_nevo, -raw_nevo) - -# Combining all dfs -hum_nutr_all = bind_rows(food_old_match, food_psf_new, food_asf) - -# check = full_join(hum_nutr_all, proc_new, by = c("product"="proc_out")) -write_csv(hum_nutr_all, "Input_data/hum_nutr_all.csv") - - -# Animal nutrition new ---------------------------------------------------- -# Cifos sheet -dat_animnutr = readxl::read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Animal_Nutr") -# %>% janitor::row_to_names(1) - -# Which procouts are needed? -# Checking what is missing in the humann nutrition shheet so that we can add what is missing from the RIVM sheet -dat_animal_nutr_cifos = dat_animnutr %>% - janitor::row_to_names(1) %>% - janitor::clean_names() %>% - mutate(product_lc = tolower(product)) %>% - relocate(product_lc, .before = "product") - -dat_animal_nutr_cifos %>% colnames(.) %>% as_tibble() %>% - write_csv("Input_data/dat_animal_cifos.csv") - -dat_miss_animal = dat_animal_nutr_cifos %>% - # dplyr::select(c(4:5), product_lc) %>% - right_join(., (dat_proc_new %>% dplyr::select(proc_out, dietary_products)), - by = c("product_lc"="proc_out"))%>% - dplyr::filter(is.na(product)) - -# Substract the procouts that are already in the old animalnutrient file -# Manual mapping: Which crops that are missing in the join above are already in the old animal nutrition sheet? -# File used as input: -# write_csv(dat_miss_animal %>% dplyr::select(product_lc, product), "Input_data/dat_miss_animal.csv") -mapd_animnutr_missing = read_csv("Input_data/dat_miss_animal_out_mapd.csv") - -# Missing animal procouts that need to be added from various sources -proc_out_anim_miss_tbl = mapd_animnutr_missing %>% - dplyr::filter(feed %in% c("feed", "grass") & - is.na(proc_out_old_animsheet)) %>% - dplyr::select(-proc_out_old_animsheet) - -proc_out_anim_miss_vec = proc_out_anim_miss_tbl %>% pull(proc_out_new_missing) - - -# Function that does all the foratting, cacluation and extraction of the cvb data from downloaded excel files -FunAnimDatExtract = function(dat, cvb_code, cvb_name, product){ - - # function that subsets the cvb data sheets based on the row headers (caution: indexed subsetting did not work due to differences in rownumbers between sheets) - FunSubsetCVB = function(dat, char_upper, char_lower, suffix_char=NULL){ - table_subset = cvb_table[which.max(cvb_table$nutrient == char_upper) : which.max(cvb_table$nutrient == char_lower), ,drop = FALSE] - table_subset = table_subset %>% dplyr::filter(nutrient != char_lower & nutrient != char_upper) - table_subset = table_subset %>% mutate(nutrient= paste(nutrient, suffix_char, sep = "")) %>% dplyr::filter(!unit == "kcal/kg - in DM") - return(table_subset) - } - - # cvb_table = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 23 57 PM.xlsx", col_names = F) #%>% - - cvb_table = dat %>% rename(nutrient = ...1, - fresh = ...2, - dm = ...3, - unit = ...4) %>% - filter(if_any(everything(), ~ !is.na(.))) - - - # Creating the subsets - weende = FunSubsetCVB(dat = cvb_table, char_upper = "Weende analysis", char_lower = "Minerals and trace elements", suffix_char = NULL) - mineral = FunSubsetCVB(dat = cvb_table, char_upper = "Minerals and trace elements", char_lower = "Fermentation products", suffix_char = NULL) - ferment = FunSubsetCVB(dat = cvb_table, char_upper = "Fermentation products", char_lower = "Digestibility coefficients ruminants", suffix_char = NULL) - - digest_rum = FunSubsetCVB(dat = cvb_table, char_upper = "Digestibility coefficients ruminants", char_lower = "Digestibility coefficients pigs", suffix_char = "r") - digest_rum2 = FunSubsetCVB(dat = cvb_table, char_upper = "DVE/OEB 2007", char_lower = "Feeding values ruminants", suffix_char = "r") - digest_pig = FunSubsetCVB(dat = cvb_table, char_upper = "Digestibility coefficients pigs", char_lower = "Digestibility coefficients roosters and laying hens",suffix_char = "p") - digest_lay = FunSubsetCVB(dat = cvb_table, char_upper = "Digestibility coefficients roosters and laying hens", char_lower = "Digestibility coefficients broilers",suffix_char = "l") - digest_bro = FunSubsetCVB(dat = cvb_table, char_upper = "Digestibility coefficients broilers", char_lower = "Digestibility coefficients rabbits", suffix_char = "b") - feeding_rum = FunSubsetCVB(dat = cvb_table, char_upper = "Feeding values ruminants", char_lower = "Feeding values rabbits", suffix_char = "r") - feeding_lay = FunSubsetCVB(dat = cvb_table, char_upper = "Feeding values roosters/laying hens", char_lower = "Feeding values broilers", suffix_char = "l") - feeding_bro = FunSubsetCVB(dat = cvb_table, char_upper = "Feeding values roosters/laying hens", char_lower = "Feeding values broilers", suffix_char = "b") - feeding_bro = FunSubsetCVB(dat = cvb_table, char_upper = "Feeding values broilers", char_lower = "Feeding values pigs", suffix_char = "b") - feeding_pig = FunSubsetCVB(dat = cvb_table, char_upper = "Feeding values pigs", char_lower = "Standardized digestibility coefficients amino acids pigs", suffix_char = "p") - - amino_acids_pigs = FunSubsetCVB(dat = cvb_table, char_upper = "Standardized digestible amino acid contents pigs", char_lower = "Apparent digestible amino acid contents pigs", suffix_char = "p") - amino_acids_bro = FunSubsetCVB(dat = cvb_table, char_upper = "Standardized digestible amino acid contents poultry", char_lower = "Fatty acids", suffix_char = "b") - amino_acids_lay = FunSubsetCVB(dat = cvb_table, char_upper = "Standardized digestible amino acid contents poultry", char_lower = "Fatty acids", suffix_char = "l") - - # Combining the data - dat_comb_cvb = bind_rows(weende,mineral,ferment,digest_rum,digest_rum2, digest_pig,digest_lay,digest_bro, - feeding_rum, feeding_lay,feeding_bro,feeding_pig,amino_acids_pigs,amino_acids_bro,amino_acids_lay) - - # matching the data - dat_cvb_final = - read_csv("Input_data/dat_animal_cifos_cvb_map.csv") %>% - dplyr::select(c(1:4)) %>% - dplyr::left_join(dat_comb_cvb, by = c("adj_col_cvb"="nutrient"))%>% - dplyr::rename( - content = dm, - unit = unit, - nutrient = cifos_cols) %>% - dplyr::select(nutrient,content) %>% - tidyr::pivot_wider(names_from = nutrient, - values_from = content) %>% - dplyr::mutate(cvb_code = !!cvb_code, - cvb_name = !!cvb_name, - product = !!product) %>% - relocate(cvb_name, .after = cvb_code) %>% - relocate(cvb_code, .before = DM) %>% - relocate(cvb_name, .after = cvb_code) %>% - relocate(product, .after = cvb_name) %>% - clean_names() %>% - dplyr::mutate(origin = "co-product", - family = NA) %>% - relocate(origin, .before = product) %>% - relocate(family, .before = product)%>% - # Calculations - #[F.H11] GE (kJ/kg) = 24.14 x CP + 36.57 x CFAT + 20.92 x CF + 16.99 x NFE - 0.63 x SUG* - dplyr::mutate( - - ge = case_when( - sug > 80 ~ round((cp*24.14+cfat*36.57+cf*13.81+nfe*16.99-0.63*sug)/1000,1), - sug <= 80 ~ round((cp*24.14+cfat*36.57+cf*13.81+nfe*16.99-0.63)/1000,1)), - - # #[F.H12] ME (kJ/kg) from cvb feed table 2018 - # me = case_when( - # sug > 80 ~ round((15.90 * dcp + 37.66 * dcfat + 13.81 * dcf + 14.64 *dnfe - 0.63 * sug)/1000,1), - # sug <= 80 ~ round((15.90 * dcp + 37.66 * dcfat + 13.81 * dcf + 14.64 *dnfe - 0.63)/1000,1)), - # - - # Calculating nitrogen from protein - n = cp/6.25) -} - -# Extract and format from cvb feed calculator: https://vvdb.cvbdiervoeding.nl/Manage/Tools/VwCalc.aspx -{ - soyabean_hulls = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 23 57 PM.xlsx", col_names = F) %>% #CF 320-360 g/kg - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "soyabean_hulls") - - soyabean_feed_hulls = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 23 57 PM.xlsx", col_names = F) %>% #CF 320-360 g/kg - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "soyabean_feed_hulls") - - - coconuts_cake_copra = read_xlsx("Input_data/CVB_procout/Copra cake (3015.401)_12_15_2021 6 35 06 PM.xlsx", col_names = F) %>% #CFAT > 100g/kg - FunAnimDatExtract(cvb_code = "3015.401" , - cvb_name = "Copra cake", - product = "coconuts_cake_copra") - - cow_peas_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% #CF 320-360 g/kg - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "cow_peas_dry_bran") - - pearl_millet_bran = read_excel("Input_data/CVB_procout/Rice bran meal, solvent extracted (1003.416).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1003.416" , - cvb_name = "Rice bran meal", - product = "pearl_millet_bran") - - oil_palm_kernels_cake = read_xlsx("Input_data/CVB_procout/Palm kernel expeller (3001.401)_12_15_2021 6 54 41 PM.xlsx", col_names = F) %>% #CF >180 g/kg - FunAnimDatExtract(cvb_code = "3001.401" , - cvb_name = "Palm kernel expeller", - product = "oil_palm_kernels_cake") - - - sorghum_bran = read_xlsx("Input_data/CVB_procout/Rice bran meal, solvent extracted (1003.416).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1003.416" , - cvb_name = "Rice bran meal", - product = "sorghum_bran") - - - bagasse = read_xlsx("Input_data/CVB_procout/Sugarbeet pulp, pressed, ensiled (4004.244).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4004.244" , - cvb_name = "Sugarbeet pulp", - product = "sugarcane_bagasse") - - - chickpea_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "chickpea_bran") - - beans_dry_bran =read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "beans_dry_bran") - - - other_pulses_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "other_pulses_bran") - - lentils_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "lentils_bran") - - - pigeon_peas_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "pigeon_peas_bran") - - - cow_peas_dry_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "cow_peas_dry_bran") - - - groundnut_cake = read_xlsx("Input_data/CVB_procout/Groundnut expeller (2013.401).xlsx", col_names = F) %>% #partly dehulled CF75 - 145 g/kg - FunAnimDatExtract(cvb_code = "2013.401" , - cvb_name = "Groundnut expeller", - product = "groundnut_cake") - - - # other_cereals_bran = read_xlsx("Input_data/CVB_procout/Rice bran meal, solvent extracted (1003.416).xlsx", col_names = F) %>% - # FunAnimDatExtract(cvb_code = "1003.416" , - # cvb_name = "Rice bran meal", - # product = "other_cereals_bran") - - olive_cake = read_xlsx("Input_data/CVB_procout/Sunflower seed meal, solvent extracted (3003.407).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3003.407" , - cvb_name = "Sunflower seed meal, solvent extracted", - product = "olive_cake") - - maize_bran = read_xlsx("Input_data/CVB_procout/Maize bran (1002.108).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1002.108" , - cvb_name = "Maize bran", - product = "maize_bran") - - maize_cake = read_xlsx("Input_data/CVB_procout/Maize germs expeller (1002.417).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1002.417" , - cvb_name = "Maize germs expeller", - product = "maize_cake") - - maize_silage = read_xlsx("Input_data/CVB_procout/Maize silage (5008.602).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5008.602" , - cvb_name = "Maize silage", - product = "maize_silage") - - grass_rangeland_fresh = read_xlsx("Input_data/CVB_procout/Grass hay, a) poor quality (5010.701).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.701" , - cvb_name = "Grass hay, a) poor quality", - product = "grass_rangeland_fresh") - - grass_pasture_fresh = read_xlsx("Input_data/CVB_procout/Grass hay, b) average quality (5010.702).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.702" , - cvb_name = "Grass hay, b) average quality", - product = "grass_pasture_fresh") - - grass_arable_fresh = read_xlsx("Input_data/CVB_procout/Grass hay, c) good quality (5010.703).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.703" , - cvb_name = "Grass hay, c) good quality", - product = "grass_arable_fresh") - - - - alfalfafor_fresh = read_xlsx("Input_data/CVB_procout/Lucerne (alfalfa), hay (5004.606).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5004.606" , - cvb_name = "Lucerne (alfalfa), hay", - product = "alfalfafor_fresh") - - - cloverfor_fresh = read_xlsx("Input_data/CVB_procout/Clover red, hay (5003.606).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5003.606" , - cvb_name = "Clover red, hay", - product = "cloverfor_fresh") - - beetfor_leaves = read_xlsx("Input_data/CVB_procout/Sugarbeet leaves, fresh (4004.642).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4004.642" , - cvb_name = "Sugarbeet leaves, fresh", - product = "beetfor_leaves") - - beetfor_root = read_xlsx("Input_data/CVB_procout/Fodderbeets, fresh (4005.000).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4005.000" , - cvb_name = "Fodderbeets, fresh", - product = "beetfor_root") - - cabbagefor_plant = read_xlsx("Input_data/CVB_procout/Cabbage (red_white_sav.), fresh (6023.000).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "6023.000" , - cvb_name = "Cabbage (red_white_sav.), fresh", - product = "cabbagefor_plant") - - # carrots_fresh = read_xlsx("Input_data/CVB_procout/Carrots (4006.000).xlsx", col_names = F) %>% - # FunAnimDatExtract(cvb_code = "4006.000" , - # cvb_name = "Carrots", - # product = "carrots_fresh") - - - carrots_tops_fresh = read_xlsx("Input_data/CVB_procout/Sugarbeet leaves with tops, fresh (4004.647).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4004.647" , - cvb_name = "Sugarbeet leaves with tops, fresh", - product = "carrots_tops_fresh") - - turnipfor_leave = read_xlsx("Input_data/CVB_procout/Sugarbeet leaves, fresh (4004.642).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4004.642" , - cvb_name = "Sugarbeet leaves, fresh", - product = "turnipfor_leave") - - - turnipfor_root = read_xlsx("Input_data/CVB_procout/Cabbage (turnip cabbage), fresh (4012.000).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4012.000" , - cvb_name = "Cabbage (turnip cabbage), fresh", - product = "turnipfor_root") - - - swedefor_leave = read_xlsx("Input_data/CVB_procout/Sugarbeet leaves, fresh (4004.642).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4004.642" , - cvb_name = "Sugarbeet leaves, fresh", - product = "swedefor_leave") - - - swedefor_root = read_xlsx("Input_data/CVB_procout/Fodderbeets, fresh (4005.000).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4005.000" , - cvb_name = "Fodderbeets, fresh", - product = "swedefor_root") - - legumenesfor_straw = read_xlsx("Input_data/CVB_procout/Bean straw (Phaseolus) (2001.508).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "2001.508" , - cvb_name = "Bean straw (Phaseolus)", - product = "legumenesfor_straw") - - # legumenesfor_bean = read_xlsx("Input_data/CVB_procout/Field beans (Vicia faba) ensiled (5001.602).xlsx", col_names = F) %>% - # FunAnimDatExtract(cvb_code = "5001.602" , - # cvb_name = "Bean straw (Phaseolus)", - # product = "legumenesfor_bean") - - # vegfor_whole = read_xlsx("Input_data/CVB_procout/Cabbage (red_white_sav.), fresh (6023.000).xlsx", col_names = F) %>% - # FunAnimDatExtract(cvb_code = "6023.000" , - # cvb_name = "Cabbage (red_white_sav.), fresh", - # product = "vegfor_whole") - - - # ryefor_fresh = read_xlsx("Input_data/CVB_procout/Rye (1007.000).xlsx", col_names = F) %>% - # FunAnimDatExtract(cvb_code = "1007.000" , - # cvb_name = "Rye", - # product = "ryefor_fresh") - - - fornesfor_plant =read_xlsx("Input_data/CVB_procout/Fodderbeets, fresh (4005.000).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4005.000" , - cvb_name = "Fodderbeets, fresh", - product = "fornesfor_plant") - - # Added after revision - coconuts_oil_copra = read_xlsx("Input_data/CVB_procout/Fat_oil, Coconut oil (3015.421)_1_18_2022 1 41 16 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3015.421" , - cvb_name = "Fat_oil, Coconut oil", - product = "coconuts_oil_copra") - - cassava_starch = read_xlsx("Input_data/CVB_procout/Tapioca starch (4008.201)_1_18_2022 2 41 14 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4008.201" , - cvb_name = "Tapioca starch", - product = "cassava_starch") - - - cassava_tapioca = read_xlsx("Input_data/CVB_procout/Tapioca, dried (4008.611)_1_18_2022 1 55 24 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4008.611" , - cvb_name = "Tapioca, dried", - product = "cassava_tapioca") - - cassava_dry = read_xlsx("Input_data/CVB_procout/Tapioca, dried (4008.611)_1_18_2022 1 55 24 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4008.611" , - cvb_name = "Tapioca, dried", - product = "cassava_dry") - - cassava_flour = read_xlsx("Input_data/CVB_procout/Tapioca, dried (4008.611)_1_18_2022 1 55 24 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4008.611" , - cvb_name = "Tapioca, dried", - product = "cassava_flour") - - pearl_millet_flour = read_xlsx("Input_data/CVB_procout/Millet, (pearl millet) (1013.000)_1_18_2022 2 01 23 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1013.000" , - cvb_name = "Millet, (pearl millet)", - product = "pearl_millet_flour") - - oil_palm_kernels_oil = read_xlsx("Input_data/CVB_procout/Fat_oil, Palm oil, chemically refined (3001.437).xlsx", col_names = F)%>% - FunAnimDatExtract(cvb_code = "3001.421" , - cvb_name = "Palmkernel oil, chemically refined", - product = "oil_palm_kernels_oil") - - oil_palm_fruits_oil = read_xlsx("Input_data/CVB_procout/Fat_oil, Palm oil, chemically refined (3001.437).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "3001.437" , - cvb_name = "Palm oil, chemically refined", - product = "oil_palm_fruits_oil") - - sorghum_flour = read_xlsx("Input_data/CVB_procout/Sorghum (1008.000)_1_18_2022 2 24 03 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1008.000" , - cvb_name = "Sorghum", - product = "sorghum_flour") - - sugarcane_molasse = read_xlsx("Input_data/CVB_procout/Molasses, sugarcane (7002.210).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "7002.210" , - cvb_name = "Molasses, sugarcane", - product = "sugarcane_molasse") - - sweet_potatoe_flour = read_xlsx("Input_data/CVB_procout/Sweet potatoes, dried (4007.611)_1_18_2022 2 30 17 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4007.611" , - cvb_name = "Sweet potatoes, dried", - product = "sweet_potatoe_flour") - - sweet_potatoe_starch = read_xlsx("Input_data/CVB_procout/Potato starch, dried (4001.201)_1_18_2022 2 32 03 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4001.201" , - cvb_name = "Potato starch, dried", - product = "sweet_potatoe_starch") - - beans_dry_vegetable = read_xlsx("Input_data/CVB_procout/Feed beans, heat treated (2001.616)_1_18_2022 2 56 41 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "2001.616" , - cvb_name = "Feed beans, heat treated", - product = "beans_dry_vegetable") - - other_tubers_flour = read_xlsx("Input_data/CVB_procout/Sweet potatoes, dried (4007.611)_1_18_2022 2 30 17 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4007.611" , - cvb_name = "Sweet potatoes, dried", - product = "other_tubers_flour") - - other_tubers_dry = read_xlsx("Input_data/CVB_procout/Sweet potatoes, dried (4007.611)_1_18_2022 2 30 17 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "4007.611" , - cvb_name = "Sweet potatoes, dried", - product = "other_tubers_dry") - - groundnut_oil = read_xlsx("Input_data/CVB_procout/Fat_oil, Groundnut oil (2013.421)_1_18_2022 3 08 32 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "2013.421" , - cvb_name = "Fat_oil, Groundnut oil", - product = "groundnut_oil") - - groundnut_butter = read_xlsx("Input_data/CVB_procout/Fat_oil, Groundnut oil (2013.421)_1_18_2022 3 08 32 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "2013.421" , - cvb_name = "Fat_oil, Groundnut oil", - product = "groundnut_butter") - - temperate_fruit_pulp = read_xlsx("Input_data/CVB_procout/Citrus pulp (6022.305)_1_18_2022 3 10 48 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "6022.305" , - cvb_name = "Citrus pulp", - product = "temperate_fruit_pulp") - - tropical_fruits_pulp = read_xlsx("Input_data/CVB_procout/Citrus pulp (6022.305)_1_18_2022 3 10 48 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "6022.305" , - cvb_name = "Citrus pulp", - product = "tropical_fruits_pulp") - - maize_germ = read_xlsx("Input_data/CVB_procout/Maize germs (1002.102)_1_18_2022 3 14 58 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1002.102" , - cvb_name = "Maize germs", - product = "maize_germ") - - maize_germ = read_xlsx("Input_data/CVB_procout/Maize germs (1002.102)_1_18_2022 3 14 58 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1002.102" , - cvb_name = "Maize germs", - product = "maize_germ") - - maize_oil = read_xlsx("Input_data/CVB_procout/Fat_oil, Maize oil (1002.421)_1_18_2022 3 16 25 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1002.421" , - cvb_name = "Fat_oil, Maize oil", - product = "maize_oil") - - grass_arable_hay = read_xlsx("Input_data/CVB_procout/Grass hay, c) good quality (5010.703)_1_18_2022 3 20 24 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.703" , - cvb_name = "Grass hay, c) good quality", - product = "grass_arable_hay") - - grass_arable_silage = read_xlsx("Input_data/CVB_procout/Grass silage, a) clay soil, before 21 June (5010.140).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.140" , - cvb_name = "Grass silage, a) clay soil, before 21 June", - product = "grass_arable_silage") - - - grass_pasture_hay = read_xlsx("Input_data/CVB_procout/Grass hay, b) average quality (5010.702)_1_18_2022 3 24 59 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.702" , - cvb_name = "Grass hay, b) average quality", - product = "grass_pasture_hay") - - - grass_pasture_silage = read_xlsx("Input_data/CVB_procout/Grass silage, j) average (5010.170)_1_18_2022 3 34 20 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.170" , - cvb_name = "Grass silage, j) average", - product = "grass_pasture_silage") - - alfalfafor_silage = read_xlsx("Input_data/CVB_procout/Lucerne (alfalfa), ensiled (5004.602)_1_18_2022 3 51 44 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5004.602" , - cvb_name = "Lucerne (alfalfa), ensiled", - product = "alfalfafor_silage") - - alfalfafor_hay = read_xlsx("Input_data/CVB_procout/Lucerne (alfalfa), hay (5004.606)_1_18_2022 3 51 06 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5004.606" , - cvb_name = "Lucerne (alfalfa), hay", - product = "alfalfafor_hay") - - cloverfor_hay = read_xlsx("Input_data/CVB_procout/Clover red, hay (5003.606)_1_18_2022 3 52 43 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5003.606" , - cvb_name = "Clover red, hay", - product = "cloverfor_hay") - - cloverfor_silage= read_xlsx("Input_data/CVB_procout/Clover red, ensiled (5003.602)_1_18_2022 3 53 06 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5003.602" , - cvb_name = "Clover red, ensiled", - product = "cloverfor_silage") - - - cloverfor_silage= read_xlsx("Input_data/CVB_procout/Clover red, ensiled (5003.602)_1_18_2022 3 53 06 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5003.602" , - cvb_name = "Clover red, ensiled", - product = "cloverfor_silage") - - - wheat_wholemeal_germ= read_xlsx("Input_data/CVB_procout/Wheat germs (1010.102)_1_18_2022 4 10 25 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "1010.102" , - cvb_name = "Wheat germs", - product = "wheat_wholemeal_germ") - - oilseedfor_hulls = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 23 57 PM.xlsx", col_names = F) %>% #CF 320-360 g/kg - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "oilseedfor_hulls") - - barley_hulls = read_xlsx("Input_data/CVB_procout/Oats husk meal (1004.111)_1_18_2022 4 13 11 PM.xlsx", col_names = F) %>% #CF 320-360 g/kg - FunAnimDatExtract(cvb_code = "1004.111" , - cvb_name = "Oats husk meal", - product = "barley_hulls") - - other_pulses_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 23 57 PM.xlsx", col_names = F) %>% #CF 320-360 g/kg - FunAnimDatExtract(cvb_code = "3012.505" , - cvb_name = "Soya bean hulls", - product = "other_pulses_bran") - - # Grass from monfreda - ryefor_fresh = read_xlsx("Input_data/CVB_procout/Grass hay, c) good quality (5010.703).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.703" , - cvb_name = "Grass hay, c) good quality", - product = "ryefor_fresh") - - ryefor_silage = read_xlsx("Input_data/CVB_procout/Grass silage, a) clay soil, before 21 June (5010.140).xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.140" , - cvb_name = "Grass silage, a) clay soil, before 21 June", - product = "ryefor_silage") - - - ryefor_dry = read_xlsx("Input_data/CVB_procout/Grass hay, c) good quality (5010.703)_1_18_2022 3 20 24 PM.xlsx", col_names = F) %>% - FunAnimDatExtract(cvb_code = "5010.703" , - cvb_name = "Grass hay, c) good quality", - product = "ryefor_hay") - - -} -# Combining all feeds -dat_anim = bind_rows(soyabean_hulls, coconuts_cake_copra, cow_peas_bran, pearl_millet_bran, - oil_palm_kernels_cake,sorghum_bran, bagasse,chickpea_bran, - beans_dry_bran,lentils_bran, pigeon_peas_bran, cow_peas_dry_bran, groundnut_cake, - # other_cereals_bran, - olive_cake, maize_bran, maize_cake,maize_silage, grass_rangeland_fresh, - grass_pasture_fresh,grass_arable_fresh, alfalfafor_fresh, - cloverfor_fresh, beetfor_leaves, beetfor_root, cabbagefor_plant, - # carrots_fresh, - carrots_tops_fresh,turnipfor_root,turnipfor_leave,swedefor_leave,swedefor_root, legumenesfor_straw, - #legumenesfor_bean, vegfor_whole, ryefor_fresh, - fornesfor_plant, coconuts_oil_copra, cassava_starch, cassava_tapioca, - cassava_dry, cassava_flour, pearl_millet_flour, oil_palm_kernels_oil, - oil_palm_fruits_oil,sorghum_flour, sugarcane_molasse, sweet_potatoe_flour, - sweet_potatoe_starch,beans_dry_vegetable, other_tubers_dry, - other_tubers_flour, groundnut_oil,groundnut_butter, - temperate_fruit_pulp,tropical_fruits_pulp,maize_germ,maize_oil, - grass_arable_hay, grass_arable_silage, grass_pasture_hay,grass_pasture_silage, - alfalfafor_silage, alfalfafor_hay, cloverfor_hay,cloverfor_silage, - soyabean_feed_hulls,wheat_wholemeal_germ,oilseedfor_hulls,barley_hulls,other_pulses_bran, - ryefor_fresh, ryefor_silage, ryefor_dry) - - -write_csv(dat_anim, "Input_Data/dat_anim.csv") - -# Combining the feed crops that are still missing and the one downloaded from cvb -dat_miss_downl = - proc_out_anim_miss_tbl %>% - left_join(dat_anim, by = c("proc_out_new_missing" = "product")) %>% - filter(is.na(cvb_name)) %>% - dplyr::select(-feed) %>% - rename(product = proc_out_new_missing) %>% - relocate(product, .before = dm ) %>% - bind_rows(dat_anim,.) %>% - mutate(family = "notyet", - origin = "co-product")# %>% -# type.convert() - - - -# Now we combine that df with the one that are in the old animal nutrition sheet -prod_vec = dat_miss_downl %>% pull(product) - -dat_com_all = dat_animal_nutr_cifos %>% select(-product) %>% - rename(product = product_lc, - cvb_name = raw_nevo, - origin = na) %>% - relocate(cvb_code, .before = cvb_name) %>% - # type.convert(.) - dplyr::filter(!product %in% prod_vec) %>% - dplyr::mutate(origin = recode(origin, "Co-Product" = "co-product")) - - -# Copy paste the col format so the rbind works. Useful template: dfWrong[] <- mapply(FUN = as,dfWrong,sapply(dfCorrect,class),SIMPLIFY = FALSE) -dat_com_all[] <- mapply(FUN = as,dat_com_all,sapply(dat_miss_downl,class),SIMPLIFY = FALSE) - -dat_comb_all = bind_rows(dat_com_all, dat_miss_downl ) - -# This contains all the NEW proc_outs -procnew_vec = proc_new %>% pull(proc_out) - -# Co-products ------------------------------------------------------------- -# Df for co-products - allprocouts for which we have data -dat_old_proc_crop_grass = - dat_animal_nutr_cifos %>% - right_join(., mapd_animnutr_missing %>% - filter(!proc_out_new_missing %in% "sugar_factorylime"), - by = c("product" = "proc_out_old_animsheet")) %>% - drop_na(product_lc) %>% - rename(origin = na) %>% - dplyr::select(-c("product_lc", "product","feed")) %>% - relocate(proc_out_new_missing, .before = "dm") %>% - rename(product = proc_out_new_missing) - -# Pasting the formating -dat_old_proc_crop_grass[] <- mapply(FUN = as,dat_old_proc_crop_grass,sapply(dat_miss_downl,class),SIMPLIFY = FALSE) - -dat_old_proc_crop_grass_sum = - dat_old_proc_crop_grass %>% - dplyr::group_by(raw_nevo, cvb_code , origin, family, product) %>% - dplyr::summarise_if(is.numeric, mean, na.rm = T) %>% - dplyr::mutate(origin = recode(origin, "Co-Product" = "co-product"), - origin = recode(origin, "Foodwaste" = "co-product")) - - - - -# All procouts that directly matched the new lower case procouts -dat_match_dir = dat_animal_nutr_cifos %>% - # dplyr::select(c(4:5), product_lc) %>% - right_join(., (dat_proc_new %>% dplyr::select(proc_out, dietary_products)), - by = c("product_lc"="proc_out"))%>% - dplyr::filter(!is.na(product)) %>% - dplyr::select(-c("product", "dietary_products")) %>% - dplyr::rename("product" = "product_lc") %>% - rename(origin = na) %>% - dplyr::mutate(origin = recode(origin, "Co-Product" = "co-product"), - origin = recode(origin, "Foodwaste" = "co-product"), - origin = recode(origin, "HarvestWaste" = "co-product")) - - -dat_match_dir[] <- mapply(FUN = as,dat_match_dir,sapply(dat_miss_downl,class),SIMPLIFY = FALSE) - - -# These are all the newly added procouts -dat_new_proc_crop_grass = - dat_miss_downl - - -# all co_products that are ASF (animal source food) -dat_fish_meat_milk = dat_animal_nutr_cifos %>% - dplyr::filter(family %in% c("Fish", "Milk", "Meat", "Egg", "Dairy")) %>% - dplyr::filter(na == "Co-Product") %>% - dplyr::select(-c("product_lc")) %>% - rename(origin = na) %>% - dplyr::mutate(origin = recode(origin, "Co-Product" = "co-product")) - -dat_fish_meat_milk[] <- mapply(FUN = as,dat_fish_meat_milk,sapply(dat_miss_downl,class),SIMPLIFY = FALSE) - -# Combining all the co-products -animal_coproducts = bind_rows(dat_old_proc_crop_grass_sum, dat_new_proc_crop_grass, dat_match_dir, dat_fish_meat_milk) %>% - dplyr::group_by(product) %>% - dplyr::summarise_if(is.numeric, mean, na.rm = T) %>% - dplyr::mutate(origin = "co-product", - raw_nevo = "crop_agg", - cvb_code = "crop_agg", - family = "none") %>% - relocate(product, .before = "dm") %>% - relocate(family, .before = "product") %>% - relocate(origin, .before = "family") %>% - relocate(cvb_code, .before = "origin") %>% - relocate(raw_nevo, .before = "cvb_code") - - -# Checking the consistency with all possible procouts - -# Check consistency needded -> https://stackoverflow.com/questions/13774773/check-whether-values-in-one-data-frame-column-exist-in-a-second-data-frame -# A$C[!A$C %in% B$C] # returns all values of A$C that are NOT in B$C -animal_coproducts$product[!animal_coproducts$product %in% proc_new$proc_out] -# All new procouts are in the df - - - -# HarvestWaste ------------------------------------------------------------ -# Df for harvest waste - cvb data from every crop we have -crop_harvestwaste = - proc_new %>% - # dat_comb_all %>% - # left_join(., proc_new, by = c("product"="proc_out")) %>% - dplyr::select(-c(proc_raw, proc_in , value , dietary_products)) %>% - # distinct_at(vars(crop_cifos), .keep_all = T) %>% - left_join(., dat_comb_all , by=c("proc_out" = "product")) %>% - dplyr::group_by(crop_cifos) %>% - dplyr::summarise_if(is.numeric, mean, na.rm = T) %>% - dplyr::mutate(raw_nevo = "crop_agg", - cvb_code = "crop_agg", - origin = "harvest_waste", - family = "none", - product = crop_cifos) %>% - relocate(product, .before = "dm") %>% - relocate(family, .before = "product") %>% - relocate(origin, .before = "family") %>% - relocate(cvb_code, .before = "origin") %>% - relocate(raw_nevo, .before = "cvb_code") %>% - dplyr::select(-crop_cifos) %>% - dplyr::filter(!product %in% c("other_fibres", "seed_cotton", "tobacco", - "tea")) -# Validation: should be 59 crops minus the unedible crops: 55 crops included - - -# proc_new$crop_cifos[!proc_new$crop_cifos %in% crop$crop_cifos] -# proc_new$crop_cifos[!proc_new$crop_cifos %in% crop$crop_cifos] -# [1] "tea" "other_fibres" "seed_cotton" "tobacco" - -# Harveste waste from ASF -asf_harvestwaste = dat_animal_nutr_cifos %>% - dplyr::filter(family %in% c("Fish", "Milk", "Meat", "Egg", "Dairy")) %>% - dplyr::filter(na == "HarvestWaste") %>% - dplyr::select(-c("product_lc")) %>% - rename(origin = na) %>% - dplyr::mutate(origin = recode(origin, "HarvestWaste" = "harvest_waste")) - -# align formatting -asf_harvestwaste[] <- mapply(FUN = as,asf_harvestwaste,sapply(dat_miss_downl,class), SIMPLIFY = FALSE) - -# Combining all the harvest waste -harvestwaste = bind_rows(crop_harvestwaste,asf_harvestwaste) - -# FoodWaste --------------------------------------------------------------- - -# Food waste crops - Food waste that matches directly with the co products from animal -foodwaste = hum_nutr_all %>% - distinct_at(vars(product), .keep_all = T) - -foodwaste_vec = foodwaste %>% pull(product) - -# foodwaste products that match coproducts animal -match_coprods_foodwaste = animal_coproducts %>% - dplyr::filter(product %in% foodwaste_vec) %>% - dplyr::mutate(origin = "foodwaste") - -match_coprods_foodwaste[] <- mapply(FUN = as,match_coprods_foodwaste,sapply(dat_miss_downl,class), SIMPLIFY = FALSE) - - -# Tea -tea_food_waste = - dat_animal_nutr_cifos %>% - dplyr::filter(product %in% "Tea")%>% - dplyr::mutate(na = "foodwaste") %>% - select(-product_lc) %>% - rename(origin = na) %>% - mutate(product = recode(product, "Tea" = "tea_leave")) - -tea_food_waste[] <- mapply(FUN = as,tea_food_waste,sapply(dat_miss_downl,class), SIMPLIFY = FALSE) - -match_animal = dat_animal_nutr_cifos %>% - dplyr::filter(product %in% foodwaste_vec) %>% - dplyr::mutate(na = "foodwaste") %>% - select(-product_lc) %>% - rename(origin = na) - -match_animal[] <- mapply(FUN = as,match_animal,sapply(dat_miss_downl,class), SIMPLIFY = FALSE) - - -match_rest = dat_animal_nutr_cifos %>% - dplyr::filter(product_lc %in% foodwaste_vec) %>% - dplyr::mutate(na = "foodwaste") %>% - select(-product) %>% - rename(product = product_lc) - -match_rest[] <- mapply(FUN = as,match_rest,sapply(dat_miss_downl,class), SIMPLIFY = FALSE) - - -# Bind food waste -foodwaste_all = bind_rows(match_coprods_foodwaste,match_animal, match_rest,tea_food_waste) %>% - distinct_at(vars(product), .keep_all = T) - - -# Checking what is not in the coproducts -miss_foodwaste = foodwaste_all %>% full_join(.,foodwaste, by = "product") - - -# Full animal nutrition sheet --------------------------------------------- -animal_nutrition_all = bind_rows(harvestwaste, animal_coproducts, foodwaste_all) %>% - dplyr::select(-raw_nevo, -cvb_code, -family) - -write_csv(animal_nutrition_all, "Input_data/animal_nutrition_all.csv") -read_csv("Input_data/animal_nutrition_all.csv") - - -# Loss fraction sheet ---------------------------------------------------- -dat_lossfrac = read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Loss_Fraction_New") -dat_lossfrac_join= dat_lossfrac %>% dplyr::select(c(1:10)) %>% janitor::row_to_names(1)%>% select(-ProdCat ,-Fam_SPAM) %>% - rename(product = Prod) - -loss_values = read_csv("Input_data/gusatvsson_loss_table.csv") %>% - rename(Fam_Gustavson = `...1`) - -# Crops - from animal nutrition sheet (postharvest) -crop_loss = crop_harvestwaste %>% distinct_at(vars(product)) %>% - mutate(ProdCat = "crop", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - -# Co-product - from animal nutrition sheet -coproduct_loss = animal_coproducts %>% distinct_at(vars(product)) %>% - mutate(ProdCat = "co-product", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - - -# Foodwaste - from Animal nutrition sheet -foodwaste_loss = foodwaste_all %>% distinct_at(vars(product)) %>% - mutate(ProdCat = "foodwaste", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - -# HarvestWaste -harvest_loss = crop_harvestwaste %>% distinct_at(vars(product)) %>% - mutate(ProdCat = "harvest_waste", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - -# Food - From human nutrtion sheet -food_loss = hum_nutr_all %>% distinct_at(vars(product)) %>% - mutate(ProdCat = "food", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - - -other_losses = tibble( - ProdCat = "other", - product = c('flax_fiber_and_tow', - 'seed_cotton_fibres', - 'tobacco_leaves') -) - -# Fish - from the old loss fraction sheet -fish_vec = c('Atlantic_herring', - 'Atlantic_cod', - 'Blue_whiting', - 'Atlantic_mackerel', - 'European_sprat', - 'Haddock', - 'Pollock', - 'European_plaice', - 'European_hake', - 'Atlantic_horse_mackerel', - 'Sandeels', - 'Norway_pout', - 'Norway_lobster', - 'Northern_prawn', - 'Ling', - 'European_pilchard') - -fish_loss = food_loss %>% filter(product == "alien") %>% - rbind(data.frame(product = fish_vec)) %>% - mutate(ProdCat = "fish", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - - -# MeatMilkEggs - -milkmeategg_vec = c('Milk', - 'Egg', - 'Pig_LW', - 'Dairy_LW', - 'Beef_LW', - 'Broiler_LW', - 'Layer_LW', - 'Salmon_LW', - 'Tilapia_LW', - 'Sheep_LW', - 'Goat_LW') - -mmegg_loss = food_loss %>% filter(product == "alien") %>% - rbind(data.frame(product = milkmeategg_vec)) %>% - mutate(ProdCat = "meatmilkeggs", - product = product) %>% - relocate(product, .after = "ProdCat")%>% - distinct_at(vars(product), .keep_all = T) - -# Grass -grass_loss = proc_new %>% filter(grepl('grass', proc_out)) %>% - distinct(proc_out) %>% - mutate(ProdCat = "grass", - product = proc_out) %>% - relocate(product, .after = "ProdCat") %>% - select(-proc_out) %>% - distinct_at(vars(product), .keep_all = T) - - -loss_fraction_1 = bind_rows(crop_loss, coproduct_loss,foodwaste_loss,harvest_loss, - food_loss,fish_loss,mmegg_loss, grass_loss,other_losses) - -# Fuzzy join -loss_frac_join = loss_fraction_1 %>% drop_na(product) %>% group_by(ProdCat) %>% - stringdist_left_join(dat_lossfrac_join %>% drop_na(product), - by = c("product"), - max_dist = 4) %>% - distinct_at(vars(ProdCat, product.x), .keep_all = T) -# write_csv(loss_frac_join, "Input_data/lossfraction_mapping_in.csv") - -# MANUAL MAPPING required - I mapped the ProdFam and Fam_Gustavson from the old loss fraction sheet. -loss_frac_all = read_csv("Input_data/lossfraction_mapping_done.csv") %>% - dplyr::select(ProdCat, product.x,ProdFam,Fam_Gustavson) %>% - left_join(loss_values, by = "Fam_Gustavson") %>% - rename(Product = product.x, - Postharvest = `Postharvest handling & storage`, - ProcessingPackaging = `Processing and packaging`, - Distribution = Distribution , - Consumption = Consumption , - Feeding = Feeding) %>% - relocate(Product, .after = "Fam_Gustavson") - -write_csv(loss_frac_all, "Input_data/loss_frac_all.csv") - - - - -# Consistency checks ----------------------------------------------------- -# RULE: What is in the procout ani nutrition should be in the processing and the loss fraction sheet product -# A$C[!A$C %in% B$C] # returns all values of A$C that are NOT in B$C - -# All values of animal nutrition that are not in loss fraction -animal_nutrition_all$product[!animal_nutrition_all$product %in% loss_frac_all$Product] -# NONE --> - -# All values of human nutrition that are not in loss fraction -hum_nutr_all$product[!hum_nutr_all$product %in% loss_frac_all$Product] -# [1] "NONE" --> GOOD! - -# All values of animal nutrition that are not in processing fraction -animal_coproducts$product[!animal_coproducts$product %in% Processing_sheet_final$proc_out] -# [1] None! Good - -# All values of humanutrition that are not in processing sheet -hum_nutr_all$product[!hum_nutr_all$product %in% Processing_sheet_final$proc_out] -# None -- Good! - -# All values that of processing that are not in loss fraction -Processing_sheet_final$proc_out[!Processing_sheet_final$proc_out %in% loss_frac_all$Product] -# None! - -# All the values that are in the loss fraction but not in the processing -loss_frac_all$Product[!loss_frac_all$Product %in% rbind(Processing_sheet_final$proc_out, Processing_sheet_final$proc_raw)] -# NONE --> GOOOD! - - -# Fert_suitability sheet -------------------------------------------------- -dat_fertsuitability = read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Fert_Suitability") %>% - dplyr::rename(scenario=1,crop_cifos = 2) - -table_fert_suit = dat_fertsuitability %>% distinct_at(vars(scenario), .keep_all = T) %>% - dplyr::select(-crop_cifos) - -# List of all crops possible -crop_list = proc_new %>% distinct_at(vars(crop_cifos)) %>% pull() - -scen_baseline = tibble(scenario = "Baseline", - crop_cifos = crop_list) - -scen_circular = tibble(scenario = "Circular", - crop_cifos = crop_list) - -fert_suit = bind_rows(scen_baseline, scen_circular) %>% - left_join(table_fert_suit, by= "scenario") - -write_csv(fert_suit, "Input_data/fert_suitability.csv") - -# Import sheet ----------------------------------------------------------- -dat_ImportExport = read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Import_Export") %>% - dplyr::select(c(1:3)) - -# Country map for subsetting EU -country_map_FAO_code = read.csv(here::here("Input_data", "Mappings", "dat_map_SPAM_GAUL_CIFOS_FAO.csv")) %>% - as_tibble() %>% dplyr::select(country_code_FAO, iso3_SPAM ) - -# EU28 - Subsetting ------------------------------------------------------- -EU_iso3 = c("AUT","BEL", "BGR", "HRV", "CZE", "DNK", "EST","FIN", "FRA", "DEU", "GRC", "HUN", "IRL", "ITA","LVA", - "LTU", "LUX", "MLT", "NLD", "POL", "PRT","ROU", "SVK","SVN", "ESP", "SWE", "GBR", "CYP") - -country_eu = country_map_FAO_code %>% filter(iso3_SPAM %in% EU_iso3) %>% - pull(country_code_FAO) - -# country map -country_map_FAO_code_namecifos = read.csv(here::here("Input_data", "Mappings", "dat_map_SPAM_GAUL_CIFOS_FAO.csv")) %>% - as_tibble() %>% dplyr::select(country_code_FAO,name_cntr_CIFOS2021) - -# Dietary products from the processing sheet -diet_prod_dist = Processing_sheet_final %>% distinct_at(vars(dietary_products)) - -# Downloading the FAO stat Food balance sheet -FoodBalanceSheetFAO = get_faostat_bulk("FBS", "Input_data") #downloads the data bulk from faostat - -Baseline_import_export = FoodBalanceSheetFAO %>% as_tibble() %>% - dplyr::filter(year %in% c("2010":"2018"), #for a 3 year average - area_code %in% country_eu, #filtering for EU+UKD countries - element == "import_quantity" | element == "export_quantity") %>% # choosing import and export quantity in 1000 tonnes - dplyr::group_by(item, element) %>% - dplyr::summarise(value = sum(value)) %>% - dplyr::mutate(value = value *1000) %>% #converting to tonnes - pivot_wider(values_from = value, - names_from = element) %>% - rename(export_ton = export_quantity, - import_ton = import_quantity, - dietary_products=item) %>% - replace(is.na(.), 0) %>% #all the NA values (crops that are not grown or imported to the EU) are 0. - dplyr::filter(dietary_products %in% (diet_prod_dist %>% pull(dietary_products))) %>% - replace(is.na(.), 0) # The forage, fibre and tabacco crops that have no match in column item are set to 0 (no import export assumed) - -write_csv(Baseline_import_export, "Input_data/Baseline_import_export.csv") - -# Baseline production ------------------------------------------------------------- -Baseline_production = FoodBalanceSheetFAO %>% as_tibble() %>% - dplyr::filter(year %in% c("2010":"2018"), - area_code %in% country_eu, #filtering for EU+UKD countries - element == "production" ) %>% # choosing import and export quantity in 1000 tonnes - dplyr::group_by(area_code, item, element) %>% - dplyr::summarise(value = sum(value)) %>% - dplyr::mutate(value = value *1000) %>% #converting to tonnes from 1000 tonnes - pivot_wider(values_from = value, - names_from = element) %>% - rename(dietary_products=item) %>% - replace(is.na(.), 0) %>% #all the NA values (crops that are not grown or imported to the EU) are 0. - dplyr::filter(dietary_products %in% (diet_prod_dist %>% pull(dietary_products))) %>% - replace(is.na(.), 0) %>% #The forage, fibre and - left_join(country_map_FAO_code_namecifos, by= c("area_code"="country_code_FAO")) %>% - ungroup() %>% - dplyr::select(name_cntr_CIFOS2021, dietary_products, production) %>% - dplyr::rename(production_t=production, - country_cifos = name_cntr_CIFOS2021) - -write_csv(Baseline_production, "Input_data/Baseline_production.csv") - -# validation %>%distinct_at(vars(country_cifos)) # all good - 28 countries - -# Baseline_food_supply ---------------------------------------------------- -Baseline_food_supply = - FoodBalanceSheetFAO %>% as_tibble() %>% - dplyr::filter(year %in% c("2010":"2018"), #for a year average - area_code %in% country_eu, #filtering for EU+UKD countries - element=='protein_supply_quantity__g_capita_day_')%>% # unit is in g/capita/day - dplyr::group_by(area_code, item, element) %>% - dplyr::summarise(value = sum(value)) %>% - pivot_wider(values_from = value, - names_from = element) %>% - rename(dietary_products=item) %>% - replace(is.na(.), 0) %>% #all the NA values (crops that are not grown or imported to the EU) are 0. - dplyr::filter(dietary_products %in% (diet_prod_dist %>% pull(dietary_products))) %>% - replace(is.na(.), 0) %>% #The forage, fibre and - left_join(country_map_FAO_code_namecifos, by= c("area_code"="country_code_FAO")) %>% - ungroup() %>% - dplyr::select(name_cntr_CIFOS2021, dietary_products,protein_supply_quantity__g_capita_day_) %>% - dplyr::rename(food_supply_gppp=protein_supply_quantity__g_capita_day_ , - country_cifos = name_cntr_CIFOS2021) - -write_csv(Baseline_food_supply, "Input_data/Baseline_food_supply.csv") - - -# Baseline animal number -------------------------------------------------- -# Loading the animal production data sheet from FAO: -# Livestock_Crop = get_faostat_bulk("QCL", "Input_data") #downloads the data bulk from faostat -# Livestock_Crop %>% write_csv("Input_data/Livestock_Crop_QCL.csv") -Livestock_Crop = read_csv("Input_data/Livestock_Crop_QCL.csv") - -Baseline_animal_number = - Livestock_Crop %>% as_tibble() %>% - dplyr::filter(year %in% c("2010":"2018"), #for a year average - area_code %in% country_eu, #filtering for EU+UKD countries - item %in% c('Beef and Buffalo Meat','Chickens', 'Meat, Poultry',"Pigs", 'Milk, whole fresh cow'), - element %in% c("producing_animals_slaughtered", "stocks","milk_animals")) #%>% -dplyr::group_by(area_code, item, element) %>% - dplyr::summarise(value = mean(value, na.rm=T)) #%>% -pivot_wider(values_from = value, - names_from = element) #%>% -rename(crops=item) %>% - replace(is.na(.), 0) %>% #all the NA values (crops that are not grown or imported to the EU) are 0. - dplyr::filter(crops %in% dat_crop_fao_cifos_vec) %>% - replace(is.na(.), 0) %>% #The forage, fibre and - left_join(country_map_FAO_code_namecifos, by= c("area_code"="country_code_FAO")) %>% - ungroup() %>% - dplyr::select(name_cntr_CIFOS2021, crops,area_harvested) %>% - dplyr::rename(area_harvested_ha=area_harvested, - country_cifos = name_cntr_CIFOS2021) - -write_csv(Baseline_food_supply, "Input_data/Baseline_food_supply.csv") - -# Baseline crop area ------------------------------------------------------ - -# Crop map -dat_crop_fao_cifos = dat_crop_final %>% dplyr::select(crop_cifos, crop_name_fao) -dat_crop_fao_cifos_vec = dat_crop_fao_cifos %>% drop_na() %>% pull(crop_name_fao) - - - - -Baseline_area_crop_ha = Livestock_Crop %>% as_tibble() %>% - dplyr::filter(year %in% c("2010":"2018"), #for a year average - area_code %in% country_eu, #filtering for EU+UKD countries - element=='area_harvested') %>% # unit is in ha - dplyr::group_by(area_code, item, element) %>% - dplyr::summarise(value = sum(value, na.rm=T)) %>% - pivot_wider(values_from = value, - names_from = element) %>% - rename(crops=item) %>% - replace(is.na(.), 0) %>% #all the NA values (crops that are not grown or imported to the EU) are 0. - dplyr::filter(crops %in% dat_crop_fao_cifos_vec) %>% - replace(is.na(.), 0) %>% #The forage, fibre and - left_join(country_map_FAO_code_namecifos, by= c("area_code"="country_code_FAO")) %>% - ungroup() %>% - dplyr::select(name_cntr_CIFOS2021, crops,area_harvested) %>% - dplyr::rename(area_harvested_ha=area_harvested, - country_cifos = name_cntr_CIFOS2021) - -write_csv(Baseline_area_crop_ha, "Input_data/Baseline_area_crop_ha.csv") - - -# Crop_others sheet ------------------------------------------------------- -# crop_other_old = read_excel("C:/Wolfram_Admin/GAMS/EU_model_frmSep21/cifos-model_eu/European_CiFoS_model_data.xlsx", sheet = "Crop_Other") -# crop_other_old %>% write_csv("Input_data/crop_other.csv") -crop_other_old = read_csv("Input_data/crop_other.csv") - -# Crop map -crop_map = read_csv("Input_data/dat_crop_with_grass.csv") - -crop_other_new = - crop_other_old %>% dplyr::select(-c(27:52, DM:HI)) %>% - dplyr::rename(crop_cifos_old = `...1`, - Rotation=`Rotation...26`) %>% - full_join(crop_map %>% dplyr::select(old_cifos_crop, crop_cifos), by = c("crop_cifos_old"="old_cifos_crop")) %>% - relocate(crop_cifos , .before = crop_cifos_old) %>% - relocate(arable, .after = Rotation) %>% - # dplyr::filter(!grepl("Grass", crop_cifos)) - dplyr::mutate(crop_cifos = case_when(crop_cifos_old == "Grass_Managed_HQ" ~ "grass_arable", - crop_cifos_old == "Grass_Managed_MQ" ~ "grass_pasture", - crop_cifos_old == "Grass_Natural_MQ" ~ "grass_rangeland", - TRUE ~ crop_cifos)) %>% - dplyr::select(-crop_cifos_old, -code_spam2010) %>% - distinct_at(vars(crop_cifos),.keep_all = T) %>% drop_na(crop_cifos) - -write_csv(crop_other_new, "Input_Data/crop_other_new.csv") - -# cropnutr = read_excel('C:/Wolfram_Admin/GAMS/EU_model_frmSep21/cifos-model_eu/European_CiFoS_model_data.xlsx',sheet="CropNutr") -# write_csv(cropnutr,"Input_data/cropnutr_old") -cropnutr = read_csv("Input_data/cropnutr_old") - -# match the new crops -cropnutr_new = - cropnutr %>% dplyr::rename(crop_cifos_old = `...1`) %>% - full_join(crop_map %>% dplyr::select(old_cifos_crop, crop_cifos), by = c("crop_cifos_old"="old_cifos_crop")) %>% - relocate(crop_cifos, .before = crop_cifos_old) %>% - dplyr::select(-crop_cifos_old) - -write_csv(cropnutr_new, "Input_data/cropnutr_new.csv") - -# CropFert_New NOT DONE! ------------------------------------------------------------ -# CropFert_New = read_excel("C:/Wolfram_Admin/GAMS/EU_model_frmSep21/cifos-model_eu/European_CiFoS_model_data.xlsx", sheet = "CropFert_New") -# CropFert_New %>% write_csv("Input_data/CropFert_New.csv") -CropFert_New = read_csv("Input_data/CropFert_New.csv") - -CropFert = - CropFert_New %>% - dplyr::rename(crop_cifos_old = `...1`) %>% - full_join(crop_map %>% select(old_cifos_crop, crop_cifos), by = c("crop_cifos_old"="old_cifos_crop")) %>% - relocate(crop_cifos , .before = crop_cifos_old) %>% - pivot_wider(names_from = CropNutr, - values_from = value) - -write_csv(CropFert, "Input_data/cropfert_inter.csv") - - - -# Data maps --------------------------------------------------------------- -# Anitas excel -- differs a bit to mine -data_map = read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Data_Map") -proc_sheet_crops = bind_rows(dat_proc_new ,dat_proc_asf) #%>% write_csv("Input_data/proc_sheet_crops.csv") - -# Crops -crop_datmap = dat_proc_new %>% select(crop_cifos) %>% distinct_all() -write_csv(crop_datmap, "Input_data/crop_datmap.csv") - - -# ProcoutH and dietary products map -ProcOutH_dietProduct = Processing_sheet_final %>% distinct_at(vars(proc_out), .keep_all = T) %>% - dplyr::select(proc_out, dietary_products) %>% - dplyr::filter(proc_out %in%(hum_nutr_all %>% distinct_at(vars(product)) %>% pull())) %>% - rename(ProcOutH = proc_out) - -write_csv(ProcOutH_dietProduct, "Input_data/ProcOutH_dietProduct.csv") - -# ProcOutH only -ProcOutH =Processing_sheet_final %>% distinct_at(vars(proc_out), .keep_all = F) %>% - dplyr::select(proc_out) %>% - dplyr::filter(proc_out %in%(hum_nutr_all %>% distinct_at(vars(product)) %>% pull())) %>% - rename(ProcOutH = proc_out) - -write_csv(ProcOutH_dietProduct, "Input_data/ProcOutH.csv") - -# Crops and co-products (proc_outs) -crop_procouts = dat_proc_new %>% - dplyr::select(crop_cifos, proc_out) %>% - distinct_at(vars(crop_cifos,proc_out)) - -write_csv(crop_procouts, "Input_data/crop_procouts.csv") - -# Livestock-Co_product -# livestock_procouts = dat_proc_asf %>% -# dplyr::select(crop_cifos, proc_out) %>% -# distinct_at(vars(crop_cifos,proc_out)) -# -# write_csv(crop_procouts, "Input_data/crop_procouts.csv") - -# Crops and co-products (proc_outs) -crop_procouts_diet_products = dat_proc_new %>% - dplyr::select(crop_cifos, proc_out,dietary_products) %>% - distinct_at(vars(crop_cifos,proc_out,dietary_products)) - -write_csv(crop_procouts_diet_products, "Input_data/crop_procouts_diet_products.csv") - -# My excel -Crop_Map2 = read_excel("C:/Wolfram_Admin/GAMS/EU_model_frmSep21/cifos-model_eu/European_CiFoS_model_data.xlsx", sheet = "Data_Map") - -# Crop_Map2 = read_excel("C:/Wolfram_Admin/GAMS/EU_model_frmSep21/cifos-model_eu/European_CiFoS_model_data.xlsx", sheet = "Data_Map") -# Crop_Map2 %>% write_csv("Input_data/crop_map_mine.csv") -Crop_Map2 =read_csv("Input_data/crop_map_mine.csv") - -# This is the only map that is actually used in the model 30.01.22 -diets_animal_crop_datamap = - Crop_Map2 %>% slice(121:210) %>% - dplyr::select(c(1:2)) %>% dplyr::rename(crop_cifos = `CROPS FAO...1`, - proc_out = `Co-Product...2`) %>% - filter(proc_out != 'Grass_Natural_LQ') %>% - full_join(dat_proc_asf %>% dplyr::select(proc_out, dietary_products)) %>% - distinct_all() %>% - bind_rows(.,dat_proc_new %>% select(c(crop_cifos,4,6))) - -write_csv(animal_map, "Input_Data/diets_animal_crop_datamap.csv") - - - -# crop_animal_co_prod_dietary - - -# Logic - Whole data in the database -# In yields, area, crop_other -# 1. Crops > FAO_crops -# # in processing sheet > remove everything that is not used in the model. All products can have a max of three functions(animal feed, human food, fertilizer) -# 2. FAO_crops > Proc_Raw (processing_sheet(ProcRaw matched with crops --> Only put in crops that get a value >> delete them)) -# 3. Proc_raw > ProcIn (supply_sheets FAO) -# 4. ProcIn > ProcOut -# 5. ProcOutc > Dietary_product (group of the FAO >> that is matched to Food groups (Fruits, vegetables, fibres,...)) -# # each Proc_out have to have a or many functions (feed, fertilizer, etc.) -# 6. Proc_out > Product (Human_Nutr sheet) # function: human food -# 7. Proc_out > Product (Animal_Nutr sheet) # function:animal feed -# 8. Proc_out > Prod (Loss_fraction_new sheet) # function: Proc_raw for fertilizer OR animal feed -# 9. Proc_out > column B (fert_suitability sheet) - -# 10 . Dietary Products ("processing sheet") > Item("import sheet") -# 11. Item(import_sheet) > Dietary_product (processing fraction sheet) >> Everything that is imported in that country, everything that is exported - -# Workflow -# Processing sheet: Make sure that we only have plant based products that are represented in the crops_coproduct -### If you do not find products in the sheet that are in the crop sheet (disaggregated) we need to find the "Value" and the nutritional content. -### Or report the assumptions >> Renee and Wendy and Demy could also help. Demy is wendy's friend and is the project assistant! -### Contact Hannah when your done with PF sheet. - -# IMPORTANT: Naming: Crops > Just name it >> Use original name from SPAM and Monfreda -# Forage crops: Just assume processing fraction of 1 - -# Single mappings -# Check following. -## ProcOutH should be element of ProcOut - -## Note: Animals ok. Crops not --> Update! - -# IMPORT sheet: Already mapped! -# When you import your crop data in the processing. - -# ProcOutH (cifos procout) > DietProd (FAO supply sheet) ## we can have four products to supply the -# DietProd (DataMap) > DietaryProducts (processing fration sheet) - -# # Animals -# #Animal_yield sheet -# 1. Yields (column D row 2) -- GitLab