# extraction total production of animals ----------------------------------
fadn.tp.animals <- function(fadn.countries ) {
fadn.raw.rds.avail <- get.available.fadn.raw.rds()[COUNTRY %in% fadn.countries]
if(nrow(fadn.raw.rds.avail)==0){
cat("Raw data does not exist, converting raw data ...\n")
convert.raw(countries = fadn.countries)}
# fadn countries
# if (fadn.countries == "all") {
fadn.countries.sel = c(
"NED",
"BEL",
"BGR",
"CYP",
"CZE",
"DAN",
"DEU",
"ELL",
"ESP",
"EST",
"FRA",
"HUN",
"IRE",
"ITA",
"LTU",
"LUX",
"LVA",
"MLT",
"OST",
"POL",
"POR",
"ROU",
"SUO",
"SVE",
"SVK",
"SVN",
"UKI",
"HRV"
)
# fadn years
fadn.years.sel = c(2004:2018)
# fadn info
fadn.info.sel = c(
"ID",
"YEAR",
"COUNTRY",
"REGION",
"NUTS0",
"NUTS1",
"NUTS2",
"NUTS3",
"SYS02",
"TF14",
"TF8",
"SIZ6",
"ORGANIC"
)
# extraction variables for animal product
fadn.tp.sel <-
c(
"LBOV_CV",
"LBOV_FUV",
"LBOV_NO",
"LBOV_OV",
"LBOV_PV",
"LBOV_SV",
"PMLKGOAT_PRQ",
"PMLKSHEP_PRQ",
"PMLKBUF_TO",
"PMLKCOW_TO",
"PMLKCOWBUF_PRQ_X",
"PMLKBUF_PRQ",
"PMLKCOW_PRQ",
"PEGGHENS_PRQ_X",
"PEGGC_PRQ",
"PEGGH_PRQ",
"PEGGH_TO",
"PEGGC_TO"
)
# fadn.str.data <- load.fadn.str.rds("alex",,"all")
# after2014 <- readRDS(paste0(rds.dir, "/alex/fadn.str.after2014.all.rds"))
# before2013 <- readRDS(paste0(rds.dir, "/alex/fadn.str.before2013.all.rds"))
# fadn.str.crops <- bind_rows(before2013$crops,after2014$crops)
# load
cols.to.load.anim_prod = c(fadn.info.sel, fadn.tp.sel)
# load raw fadn data using fadnutils package
cat ("loading animal product variables from raw data\n")
tmp.LOAD.raw = fadnUtils::load.fadn.raw.rds(years = fadn.years.sel,
col.filter = cols.to.load.anim_prod)
#
tmp.LOAD.raw <- tmp.LOAD.raw %>% select(-c(load.COUNTRY, load.YEAR))
tmp.LOAD <- tmp.LOAD.raw %>% mutate(
COMI_GROF = ifelse (
YEAR %in% c(2004:2013),
PMLKCOWBUF_PRQ_X,
(PMLKBUF_PRQ + PMLKCOW_PRQ)
),
COMI_EAAP = PMLKBUF_TO + PMLKCOW_TO,
EGGS_GROF = ifelse (
YEAR %in% c(2004:2013),
PEGGHENS_PRQ_X,
as.numeric (PEGGC_PRQ + PEGGH_PRQ)
) ,
EGGS_EAAP = PEGGH_TO + PEGGC_TO,
SGMI_GROF = PMLKGOAT_PRQ + PMLKSHEP_PRQ,
PCAT_EAAP = LBOV_CV + LBOV_FUV - LBOV_OV + LBOV_NO + LBOV_PV,
PCAT_EALP = LBOV_NO
)
tmp.LOAD.raw.long = melt(tmp.LOAD %>% select(-fadn.tp.sel),
id.vars = fadn.info.sel)
tmp.LOAD.raw.long[grep("_", variable),
":="(
PRODUCT = gsub("^(.+?)_(.+?)(_.*)?$", "\\1", variable),
VAR = gsub("^(.+?)_(.+?)(_.*)?$", "\\2", variable)
)]
# rename and set the type of objects
ANIMALS.production.all <- tmp.LOAD.raw.long %>%
select(-c("NUTS0")) %>%
mutate_if(is.character, as.factor) %>%
mutate_at(c("COUNTRY", "REGION", "NUTS3", "NUTS2"), as.character) %>%
mutate(ORGANIC =
paste0('org-', ORGANIC)) %>%
select(-variable)%>%
rename(WEIGHT = SYS02, variable = VAR, ANIM = PRODUCT )
return(ANIMALS.production.all)
}
ANIMALS.production.all <- fadn.tp.animals(fadn.countries)