R/aggregate_for_variable_by_region.R

Defines functions aggregate_for_variable_by_region

Documented in aggregate_for_variable_by_region

#' @title Data aggregation function for a variable and the seven regions / case studies of the ENTRANCES project
#' @description Aggregates data for a variable and the 7 regions/case studies of the ENTRANCES data set. The name of the aggregated value is derived from the name of the list object, the variable name and the level of the NUTS_units.
#'
#' @param VarName character string with the name of the variable.
#' @param group_by "sum", "mean" or "median", indicates whether aggregation should work by using the sum, the mean or the median of the data.
#' @param NUTS_level indicates, if the nuts_ids shall be filtered on a specific NUTS level (NUTS3, NUTS2, NUTS1, NUTS country). Default is all.
#' @param data dataframe, from which you want to extract your data. Default is the EntrancesData dataset
#' @return dataframe
#' @export
#' @examples
#' #Define a list of a variable and the type of aggregation (sum, mean or median)to get a data frame with the aggregated values for each region (Entrances case studies) and each year.
#' #Additionally you can define a NUTS level for the aggregation (NUTS3, NUTS2, NUTS1, NUTS country). Default is "NUTS3".
#' #You can define a data frame, if you don't want to use the EntrancesData data set from the package.
#' aggregate_for_variable_by_region(VarName="GDP_EUR", group_by="sum", data=EntrancesData, NUTS_level="all")

aggregate_for_variable_by_region<- function(VarName, group_by="sum", data=EntrancesData, NUTS_level="NUTS3"){
  regions<-c("rhineland", "lusatia", "centralgermany", "jiuvalley", "uppernitra", "sulcis", "silesia")
  dataout<-EntrancesData[1,]
  dataout<-dataout[-1,]
  cnames<-colnames(dataout)
  for (reg in regions){
    nutsids<-get_nuts_ids_for_region(reg)
    dataout<- rbind(dataout, aggregate_for_variable_by_nuts(nuts_ids=nutsids, VarName=VarName, aggregation_name = paste("aggr", reg, NUTS_level, sep="_"), group_by=group_by, data=data, NUTS_level = NUTS_level))
    colnames(dataout)<-cnames
  }
  return(dataout)
}
THartl1/EntrancesDataPackage documentation built on Dec. 18, 2021, 4:01 p.m.