Commit eda960ca authored by Simon, Wolfram's avatar Simon, Wolfram
Browse files

Update Crop_cifos_DataPreparation_DB.R

parent be93a2a6
......@@ -6,7 +6,8 @@
# library("easypackages")
# Loading packages
easypackages::packages("odbc","DBI", "RSQLite", "dbplyr", "readxl", "fuzzyjoin",
"sqldf", "downloader", "tidyverse", "janitor", "FAOSTAT")
"sqldf", "downloader", "tidyverse", "janitor", "FAOSTAT",
"validate")
# https://www.youtube.com/watch?v=CydajdNRJOU -----------------------------
......@@ -237,18 +238,17 @@ dat_crop_final = read_csv("Input_data/dat_crop_with_grass_final.csv")
# 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)) %>%
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!
......@@ -480,6 +480,7 @@ print(new_procin, n=33)
# 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)
......@@ -505,12 +506,6 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out"
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") %>%
# cow peas -- Source: from Technical Conversion Factors for Agricultural Commodities (http://countrystat.org/resources/documents/tcf.pdf); adjusted commodity tree; fractions from "pulses"
# Levels: all pulses only one level
add_row(crop_cifos = "cow_peas_dry" , proc_raw = "cow_peas_dry" , proc_in = "cow_peas_vegetables", proc_out = "cow_peas_vegetables", value = 0.95, dietary_products = "Pulses, Other and products") %>% #5% water is assumed to be lost
add_row(crop_cifos = "cow_peas_dry" , proc_raw = "cow_peas_dry" , proc_in = "cow_peas_processed", proc_out = "cow_peas_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_processed", proc_out = "cow_peas_bran", value = 0.23, dietary_products = "Pulses, Other 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") %>%
......@@ -674,9 +669,9 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out"
## 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 = "grass_pasture_hay", value = 0.894, dietary_products = "Forage and silage, legumes") %>% #Alfalfa, hay, (feedepedia)
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 = "grass_pasture_silage", value = 0.308, dietary_products = "Forage and silage, legumes") %>% #Alfalfa, silage (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)
......@@ -686,10 +681,9 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out"
# 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_dry", 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_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
......@@ -848,7 +842,11 @@ dat_proc_new = dat_complete %>%
"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)) #all lower cases - less confusion
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")
......@@ -869,31 +867,15 @@ dat_proc_asf =
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))
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")
# Tagging utilisation of procouts (Hannah: should not be used -- we should just use all the procouts) -------------------------------------------
# Manual utilization tagging of procouts in excel
# proc_util_tag = read_csv("Input_data/dat_proc_new_tag.csv")
#
# # Human nutrition
# food_proc = proc_util_tag %>% filter(
# grepl("food", cat))
#
# # Animal nutrition
# feed_proc = proc_util_tag %>% filter(
# grepl("feed", cat) |
# grepl("grass", cat))
#
# # Fertilzation
# fert_proc = proc_util_tag %>% filter(
# grepl("fertilizer", cat))
# Human nutrition - backup -----------------------------------------------
# Human nutrition sheet ---------------------------------------------------
# Human nutrtion sheet
......@@ -923,6 +905,8 @@ dat_procout_demi = read_csv("Input_data/demi_human_food.csv") %>%
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") %>%
......@@ -997,207 +981,10 @@ food_asf = dat_humnutr %>% janitor::row_to_names(2) %>% janitor::clean_names() %
# 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")
#
#
# # Human nutrition sheet ---------------------------------------------------
# # Human nutrtion sheet
# dat_humnutr = read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Human_Nutr")
#
# # Nutritional dataset downloaded from https://www.rivm.nl/documenten/nevo-online-versie-2019-60
# # downloader::download("https://www.rivm.nl/sites/default/files/2019-11/NEVO-online%20versie-2019-60.zip",
# # dest="Input_data/NEVO-online versie-2019-60.zip", mode="wb")
# # unzip("Input_data/NEVO-online versie-2019-60.zip")
# # # key for components can be downloaded here: https://www.rivm.nl/sites/default/files/2019-11/Components%20in%20NEVO-online%202019_0.pdf
# # dat_nutr_rivm = read_excel("Input_data/NEVO-online versie-2019-60/nevo_online_2019.xlsx",
# # sheet = "NevoOnline2019 Nutrientgehaltes")
# #
# # # Checking if the variables in the human nutrition sheet are in the RIVM data sheet
# # col_rivm = colnames(dat_nutr_rivm)
# # dat_cols = dat_humnutr %>% janitor::row_to_names(2)
# # col_cifos = colnames(dat_cols) %>% as_tibble() %>% pull()
# # # join_dat = hum_nutr_cols %>% drop_na() %>% distinct() %>%
# # # stringdist_left_join(col_rivm,
# # # by = c("value" = "value"),
# # # max_dist = 3) # all the cifos human nutrition sheet are in the rivm data sheet!
# #
# # dat_map_nevo_cifos = dat_nutr_rivm %>% dplyr::select(#identifiers
# # Productgroepcode,
# # Productcode,
# # Product_description,
# # Product_synoniemen,
# #
# # #Water and Energy
# # WATER_g,
# # ENERCC_kcal,
# # # Macro nutritnes
# # PROT_g,
# # CHO_g,
# # FIBT_g,
# # FAT_g,
# # # Fatty acids
# # F18_2CN6_g, F18_3CN3_g,F20_5CN3_g,F22_6CN3_g,
# # # Cholesterol
# # CHORL_mg,
# # # minerals
# # NA_mg,K_mg,CA_mg,P_mg,MG_mg,FE_mg,CU_mg,SE_mug,ZN_mg,ID_mug,
# # #Vitammins
# # RETOL_mug,
# # THIA_mg,RIBF_mg, NIA_mg,VITB6_mg,FOL_mug,VITB12_mug,
# # VITC_mg,
# # VITD_mug,
# # VITE_mg,
# # VITK_mug,
# # # Nitrogen
# # NT_g) %>% #selecting the variables that match the cifos
# # dplyr::mutate(Weight = "100") %>% #creating missing columns from cifos hum nutr sheet
# # dplyr::rename(Product = Product_description,
# # Energy = ENERCC_kcal,
# # Protein = PROT_g,
# # N = NT_g,
# # Carbohydrates=CHO_g,
# # Water = WATER_g,
# # Fiber = FIBT_g,
# # Fat_total = FAT_g,
# # LA = F18_2CN6_g,
# # ALA = F18_3CN3_g,
# # EPA = F20_5CN3_g,
# # DHA = F22_6CN3_g,
# # Cholestrol = CHORL_mg,
# # Na = NA_mg,
# # K = K_mg,
# # Ca = CA_mg,
# # P = P_mg,
# # Mg = MG_mg,
# # Fe = FE_mg,
# # Cu = CU_mg,
# # Se = SE_mug,
# # Zn = ZN_mg,
# # I = ID_mug,
# # A = RETOL_mug,
# # B1 = THIA_mg,
# # B2 = RIBF_mg,
# # B3 = NIA_mg,
# # B6 = VITB6_mg,
# # B9 = FOL_mug,
# # B12 = VITB12_mug,
# # C = VITC_mg,
# # D = VITD_mug,
# # E = VITE_mg,
# # VitK = VITK_mug,
# # N = NT_g) %>% #Renaming the variables so they match the cifos
# # dplyr::relocate(Water, .before = Energy)
# #
# # write_csv(dat_map_nevo_cifos, "Input_data/dat_map_nevo_cifos.csv")
# #
# # # Different names in proc_out and the Product
# # dat_map_nevo_cifos %>% dplyr::filter(Productcode == 220)
# #
# # # Adding missing products from nutritional sheets
# # vec_dist_new_crops = new_dietprods_dist %>% pull(crop_cifos)
# # # [1] "grass_pasture" "ryefor" "seed_cotton" "tobacco" "cassava"
# # # "cocoa_beans" "coconuts" "pearl_millet" "oil_palm_fruit" "plantains" "sorghum"
# # # [12] "sugar_cane" "sweet_potatoes" "tea"
# #
# # test = dat_map_nevo_cifos %>%
# # filter(
# # grepl("Cassava raw", Product) |
# # grepl("rye", Product) |
# # grepl("mill", Product) | #not
# # grepl("palm", Product) |
# # grepl("plantain", Product) |
# # grepl("chocol", Product) |
# # grepl("coconut", Product) |
# # grepl("sugar", Product) |
# # grepl("potato sweet raw", Product) |
# # grepl("tea", Product) |
# # grepl("sorg", Product)|
# # grepl("Yam raw", Product)
# # )
# #
# # dat_map_nevo_cifos %>%
# # filter(
# # grepl("", Product_synoniemen ))
# #
# #
# # dat_map_nevo_cifos %>%
# # filter(Product %in% c("Cassava raw",
# # "Yam raw",
# # "Potato sweet raw",
# # "Ice tea light",
# # "Milk chocolate-flavoured full fat",
# # "Oil palm", "Oil palmkernel", # we need the processing fractions
# # "Oil coconut", #only oil in nevo
# # "Icing (sugar)"
# # ))
# #
# #
# #
# # # Nutrition sheet
# # # Removing the animal family from sheet
# #
# # # Distinct plant family groups
# # dist_family_plnts = dat_humnutr %>% janitor::row_to_names(2) %>% janitor::clean_names() %>%
# # dplyr::filter(!family %in% c("Dairy", "Fish", "Meat", "Egg")) %>% distinct_at(vars(family))
# #
# #
# # write_csv(dist_family_plnts, "Input_data/dist_family_plnts.csv")
#
#
# # 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")
#
# # 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))
#
# write_csv(proc_out_missing, "Input_data/proc_out_missing.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)
#
#
# # %>%
# full_join(., (dat_proc_new %>% dplyr::select(proc_out, dietary_products)),
# by = c("product"="proc_out"))
#
# 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")
#
# Animal nutrition new ----------------------------------------------------
# Cifos sheet
......@@ -1449,6 +1236,7 @@ FunAnimDatExtract = function(dat, cvb_code, cvb_name, product){
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",
......@@ -1603,7 +1391,6 @@ FunAnimDatExtract = function(dat, cvb_code, cvb_name, product){
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",
......@@ -1614,13 +1401,11 @@ FunAnimDatExtract = function(dat, cvb_code, cvb_name, product){
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",
......@@ -1641,13 +1426,11 @@ FunAnimDatExtract = function(dat, cvb_code, cvb_name, product){
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",
......@@ -1721,6 +1504,25 @@ FunAnimDatExtract = function(dat, cvb_code, cvb_name, product){
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,
......@@ -1741,7 +1543,8 @@ dat_anim = bind_rows(soyabean_hulls, coconuts_cake_copra, cow_peas_bran, pearl_m
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)
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")
......@@ -1915,7 +1718,7 @@ harvestwaste = bind_rows(crop_harvestwaste,asf_harvestwaste)
foodwaste = hum_nutr_all %>%
distinct_at(vars(product), .keep_all = T)
foodwaste_vec = foodwaste %>% pull(product)
foodwaste_vec = foodwaste %>% pull(product)
# foodwaste products that match coproducts animal
match_coprods_foodwaste = animal_coproducts %>%
......@@ -1954,16 +1757,6 @@ match_rest = dat_animal_nutr_cifos %>%
match_rest[] <- mapply(FUN = as,match_rest,sapply(dat_miss_downl,class), SIMPLIFY = FALSE)
# Food waste that matches the initial data
# foodwaste_old = dat_animal_nutr_cifos %>%
# dplyr::filter(product_lc %in% foodwaste) %>%
# # dplyr::mutate(na = "foodwaste") %>%
# select(-product) %>%
# rename(origin = na,
# product = product_lc)
#
# foodwaste_old[] <- mapply(FUN = as,foodwaste_old,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)
......@@ -1978,334 +1771,9 @@ 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")
# # Animal nutrition sheet -------------------------------------------------
# # Cifos sheet
# dat_animnutr = readxl::read_excel("Input_data/Copy of European_CiFoS_model_data_ANITANov21.xlsx", sheet = "Animal_Nutr")
# dat_animnutr %>% 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")
# # write_csv(dat_humnutr_cifos, "Input_data/dat_humnutr_cifos.csv")
#
# dat_miss_animal = dat_animal_nutr_cifos %>%
# # dplyr::select(c(4:5), product_lc) %>%
# right_join(., (feed_proc %>% dplyr::select(proc_out, dietary_products)),
# by = c("product_lc"="proc_out")) %>%
# dplyr::filter(is.na(product))
#
#
# # Missing animal feeds
# miss_animal_nutr = dat_miss_animal %>% pull(product_lc)
#
# # CVB sheet - data pulling function
# anm_nutr_cols = colnames(dat_animal_nutr_cifos) %>%
# as_tibble() #%>%
# # write_csv("Input_data/anm_nutr_cols.csv")
#
# cvb_cifos_map = read_csv("Input_data/anm_nutr_cols_mapd.csv") %>%
# dplyr::mutate(cvb_cols_mix = case_when(is.na(cvb_cols) ~ cifos_cols,
# TRUE ~ cvb_cols)) %>%
# dplyr::mutate(cvb_cols_mix = tolower(cvb_cols_mix))
#
# # Getting the generic row positions of the cvb variables that we need
# vec_cvb_cifos = read_csv("Input_data/anm_nutr_cols_mapd.csv")%>%
# drop_na(cvb_row_pos) %>%
# filter(!cvb_row_pos %in% c(-999,-99)) %>%
# pull(cvb_row_pos)
#
# # # Getting the procout
# # c
# # # Getting the correct values and widen data set
# # weende_soyhul = soy_hulls %>%
# # dplyr::mutate(across(c(1), tolower)) %>%
# # dplyr::slice(vec_cvb_cifos) %>%
# # dplyr::rename(content = ...2,
# # unit = ...4,
# # nutrient = `Weende analysis`) %>%
# # dplyr::select(-c(3))
# #
# # dat_crop = cvb_cifos_map %>%
# # mutate(cvb_cols = tolower(cvb_cols)) %>%
# # dplyr::left_join(., weende_soyhul, by = c("cvb_cols_mix"="nutrient")) %>%
# # distinct(cifos_cols, .keep_all = T) %>%
# # dplyr::select(cifos_cols, content) %>%
# # tidyr::pivot_wider(names_from = cifos_cols,
# # values_from = content) %>%
# # dplyr::mutate(cvb_code = cvb_code_vec)
# #
# # # Loading all the files from cvb
#
#
# # Function
# FunAnimDatExtract = function(dat_downloaded, cvb_code, cvb_name, product){
#
# dat = dat_downloaded %>%
# dplyr::mutate(across(c(1), tolower)) %>%
# dplyr::slice(vec_cvb_cifos) %>%
# dplyr::rename(content = ...2,
# unit = ...4,
# nutrient = `Weende analysis`) %>%
# dplyr::select(-c(3))
#
# dat_crop = cvb_cifos_map %>%
# mutate(cvb_cols = tolower(cvb_cols)) %>%
# dplyr::left_join(., dat, by = c("cvb_cols_mix"="nutrient")) %>%
# distinct(cifos_cols, .keep_all = T) %>%
# dplyr::select(cifos_cols, content) %>%
# tidyr::pivot_wider(names_from = cifos_cols,
# values_from = content) %>%
# dplyr::mutate(cvb_code = !!cvb_code,
# cvb_name = !!cvb_name,
# product = !!product) %>%
# relocate(cvb_name, .after = cvb_code)
# }
#
# # Extract and format
#
# soyabean_hulls = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 23 57 PM.xlsx") %>% #CF 320-360 g/kg
# FunAnimDatExtract(cvb_code = "3012.505" ,
# cvb_name = "Soya bean hulls",
# product = "soyabean_hulls")
#
# coconuts_cake_copra = read_xlsx("Input_data/CVB_procout/Copra cake (3015.401)_12_15_2021 6 35 06 PM.xlsx") %>% #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") %>% #CF 320-360 g/kg
# FunAnimDatExtract(cvb_code = "3012.505" ,
# cvb_name = "Soya bean hulls",
# product = "cow_peas_bran")
#
# pearl_millet_bran = read_excel("Input_data/CVB_procout/Rice bran meal, solvent extracted (1003.416).xlsx") %>%
# 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") %>% #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") %>%
# 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") %>%
# FunAnimDatExtract(cvb_code = "4004.244" ,
# cvb_name = "Sugarbeet pulp",
# product = "bagasse")
#
#
# chickpea_bran = read_xlsx("Input_data/CVB_procout/Soya bean hulls (3012.505)_12_15_2021 6 44 56 PM.xlsx") %>%
# 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") %>%
# 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") %>%
# 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") %>%
# FunAnimDatExtract(cvb_code = "3012.505" ,
# cvb_name = "Soya bean hulls",
# product = "lentils_bran")
#