Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
MaizCorn = weighted.mean(`misc%MaizCorn`, `misc%weights`, na.rm = TRUE),
MaizSil = weighted.mean(`misc%MaizSil`, `misc%weights`, na.rm = TRUE),
# `Annual Work Units` = median(`global%Aks`, `misc%weights`, na.rm = TRUE),
`Farm Net Value Added [EUR]` = weighted.mean(`misc%net cashflow`, `misc%weights`, na.rm = TRUE),
`Median FNVA [EUR]` = median(`misc%net cashflow`, na.rm = TRUE),
`Annual Work Units` = weighted.mean(`global%Aks`, `misc%weights`, na.rm = TRUE),
`FNVA per AWU` = `Farm Net Value Added [EUR]` / `Annual Work Units`,
N_use = weighted.mean(`misc%N_use`, `misc%weights`, na.rm = TRUE),
n = sum(`misc%nFarms`)
)
descstats <- descstats %>%
mutate(
`Share of Cereals [%]` = ((SummerCere + Winterbarley + WinterWheat) / `Land [ha]`) * 100,
`kg N per ha` = N_use * 1000 / `Land [ha]`
) %>%
select(
NUTS0, `Land [ha]`, `Share of Cereals [%]`,
`Annual Work Units`, `kg N per ha`, `Farm Net Value Added [EUR]`, n,
`Median FNVA [EUR]`, `FNVA per AWU`
) %>%
filter(n >= 15)
}
if (type == "dairy") {
descstats <- farm_data %>%
group_by(NUTS0) %>%
summarise(
nCows_mean = weighted.mean(`global%nCows`, `misc%weights`, na.rm = TRUE),
nArabLand_mean = weighted.mean(`global%nArabLand`, `misc%weights`, na.rm = TRUE),
nGrasLand_mean = weighted.mean(`global%nGrasLand`, `misc%weights`, na.rm = TRUE),
milkYield_mean = weighted.mean(`global%milkYield`, `misc%weights`, na.rm = TRUE),
`Share of Grassland [%]` = weighted.mean(`global%ShareGrassLand`, `misc%weights`, na.rm = TRUE) * 100,
# `Annual Work Units` = median(`global%Aks`, `misc%weights`, na.rm = TRUE),
`Farm Net Value Added [EUR]` = weighted.mean(`misc%net_value_added`, `misc%weights`, na.rm = TRUE),
`Median FNVA [EUR]` = median(`misc%net_value_added`, na.rm = TRUE),
`Annual Work Units` = weighted.mean(`global%Aks`, `misc%weights`, na.rm = TRUE),
`FNVA per AWU` = `Farm Net Value Added [EUR]` / `Annual Work Units`,
`Livestock density` = nCows_mean / (nArabLand_mean + nGrasLand_mean),
n = sum(`misc%nFarms`)
) %>%
filter(n >= 15)
}
if (csv == TRUE) {
descstats %>% write.csv(file.path(dir, paste0(type, "_descstats.csv")), row.names = FALSE)
}
return(descstats)
}
# Is nan function
## is.nan.data.frame ----
#' `is.nan.data.frame()` checks if there are any NaNs in a dataframe (`is.nan()` does not work for dfs)
#' @param x A dataframe
#' @return A dataframe with TRUE or FALSE for each column
#' @export is.nan.data.frame
is.nan.data.frame <- function(x) do.call(cbind, lapply(x, is.nan))
# Remove aggregated farms with less than 15 farms for reporting
## remove_aggregated_farms ----
#' `rm_lown()` removes aggregated farms with less than 15 farms for reporting
#' @param data A dataframe with the data to plot
#' @param farm_data A dataframe with the farm data
#' @return A dataframe with the data without the aggregated farms
#' @export rm_lown
rm_lown <- function(data, farm_data) {
data <- data[data$farmIds %in% farm_data[!farm_data$`misc%nFarms` < 15, ]$farmIds, ]
return(data)
}
# Find the first matching column
## first_match_col ----
#' `first_match_col()` finds the first matching column in a dataframe
#' @param x A dataframe
#' @param pattern A pattern to match
#' @param how How to match the pattern (all or any)
#' @return The name of the first matching column
#' @export first_match_col
#' @examples
#' data <- data.frame(a = c("a", "b", "c"), b = c("a", " ", "c"), c = c("a", "b", "1"))
#' first_match_col(data, "\\D", "all")
#' first_match_col(data, "\\d", "any")
first_match_col <- function(x, pattern, how = c("all", "any")) {
found <- NULL
for (i in seq_along(x)) {
if (how == "all") {
if (all(grepl(x[[i]], pattern = pattern))) {
found <- colnames(x)[i]
break
}
} else if (how == "any") {
if (any(grepl(x[[i]], pattern = pattern))) {
found <- colnames(x)[i]
break
}
}
if (i == length(x) && length(found) == 0) {
rlang::abort("No matching columns found")
}
}
return(found)
}
# Make a function that replaces the name of the column in first_match_col with what the user inputs
## replace_first_match_col ----
#' `replace_first_match_col()` replaces the name of the column in `first_match_col()` with what the user inputs
#' @inheritParams first_match_col
#' @param replace_with The name to replace the column name with
#' @return dataframe with the replaced column name
#' @export replace_first_match_col
#' @examples
#' data <- data.frame(a = c("a", "b", "c"), b = c("a", " ", "c"), c = c("a", "b", "1"))
#' replace_first_match_col(data, "\\D", "all", "new")
#' replace_first_match_col(data, "\\d", "any", "new")
replace_first_match_col <- function(x, pattern, how = c("all", "any"), replace_with) {
first_match_col(x, pattern, how) -> found
colnames(x)[colnames(x) == found] <- replace_with
return(x)
}