diff --git a/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R b/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R index 1a74574a4f80a2db996ac85100b9a3baaecd1e09..95ef88ae5f12a6931fbeaded15bbf56388683c3b 100644 --- a/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R +++ b/R_to_SQL_database/Crop_cifos_DataPreparation_DB.R @@ -1,12 +1,11 @@ - -# Loading packages -------------------------------------------------------- +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", + "sqldf", "downloader", "tidyverse", "janitor", "FAOSTAT", "validate") # https://www.youtube.com/watch?v=CydajdNRJOU ----------------------------- @@ -103,18 +102,18 @@ easypackages::packages("odbc","DBI", "RSQLite", "dbplyr", "readxl", "fuzzyjoin", # 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_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) + .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 ------------------------------------- @@ -132,23 +131,23 @@ mapping_grasscrops = read_csv("Input_data/mapping_grasscrops_new.csv") %>% 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)) %>% + 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")) + "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. @@ -164,9 +163,9 @@ 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) +# 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) # @@ -244,7 +243,7 @@ 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 %>% @@ -261,12 +260,12 @@ dat_proc = Processing_sheet %>% str_detect(crop_cifos, "grass") ~ crop_cifos, crop_cifos == "tobacco" ~ crop_cifos, crop_cifos == "seed_cotton" ~ crop_cifos, - TRUE ~ .)), + 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 ~ .))) + TRUE ~ .))) # removing disaggregated group elemetns that are not needed - # Aim: To have only one surrogate crop per crop_group (temperate_fruits = Apples) @@ -293,7 +292,7 @@ dat_2nd_level2 = anti_join(Processing_sheet,dat_2nd_level, by = "ProRaw") %>% "barley", "barley")) %>% relocate(crop_cifos, .before = 1) %>% clean_names() - + dat_group = sqldf('SELECT * FROM dat_proc WHERE @@ -325,7 +324,7 @@ dat_group = sqldf('SELECT * FROM dat_proc WHERE 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 @@ -382,7 +381,7 @@ dat_dietprod_comp = dat_proc_2 %>% 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) @@ -400,14 +399,14 @@ dat_complete = dat_dietprod_comp %>% 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") %>% @@ -439,7 +438,7 @@ new_procin = dat_complete %>% # - 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 @@ -498,14 +497,14 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" 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") %>% @@ -538,12 +537,12 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" 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 -- + + # tea -- add_row(crop_cifos = "tea" , proc_raw = "tea" , proc_in = "teain", proc_out = "tea_leave", value = 1, dietary_products = "Tea (including mate)") %>% # yams -- @@ -613,12 +612,12 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" 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)") %>% @@ -666,7 +665,7 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" ## 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) @@ -702,7 +701,7 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" # 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 @@ -769,7 +768,7 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" 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 + # 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") %>% @@ -784,11 +783,11 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" # 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 = "soyabean_for_oil", proc_out = "sunflower_cake", value = 0.47, dietary_products = "Soyabeans") %>% - add_row(crop_cifos = "sunflower" , proc_raw = "sunflower_seed" , proc_in = "soyabean_for_oil", proc_out = "sunflower_hulls", value = 0.07, dietary_products = "Soyabeans") %>% #ref from ollie (old proccessing sheet) + 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 + # 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 @@ -803,15 +802,15 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" # 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 + + # 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 @@ -824,7 +823,7 @@ proc_new = tibble("crop_cifos" = NA, "proc_raw" = NA, "proc_in" = NA, "proc_out" # 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) @@ -860,22 +859,22 @@ dat_proc_new %>% write_csv("Input_data/dat_proc_new.csv") dat_proc_asf = Processing_sheet %>% dplyr::select(-c(6,7)) %>% - clean_names() %>% rename(proc_raw = pro_raw) %>% - slice(269:363) %>% + 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 + 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 @@ -984,7 +983,7 @@ 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 @@ -1939,9 +1938,6 @@ loss_frac_all$Product[!loss_frac_all$Product %in% rbind(Processing_sheet_final$p # 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) @@ -1951,18 +1947,18 @@ table_fert_suit = dat_fertsuitability %>% distinct_at(vars(scenario), .keep_all # List of all crops possible crop_list = proc_new %>% distinct_at(vars(crop_cifos)) %>% pull() - + scen_baseline = tibble(scenario = "Baseline", - crop_cifos = crop_list) + crop_cifos = crop_list) scen_circular = tibble(scenario = "Circular", - crop_cifos = crop_list) + 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)) @@ -2002,7 +1998,7 @@ Baseline_import_export = FoodBalanceSheetFAO %>% as_tibble() %>% 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) + 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") @@ -2065,11 +2061,11 @@ Baseline_animal_number = 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::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) %>% +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 @@ -2130,12 +2126,12 @@ crop_other_new = 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)) %>% + 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") @@ -2146,14 +2142,14 @@ cropnutr_new = 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") + +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`) %>% @@ -2174,7 +2170,7 @@ proc_sheet_crops = bind_rows(dat_proc_new ,dat_proc_asf) #%>% write_csv("Input_ # 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) %>% @@ -2224,15 +2220,15 @@ Crop_Map2 =read_csv("Input_data/crop_map_mine.csv") 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`) %>% + 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 @@ -2278,14 +2274,3 @@ write_csv(animal_map, "Input_Data/diets_animal_crop_datamap.csv") # # Animals # #Animal_yield sheet # 1. Yields (column D row 2) - - - - - - - - - - -