R/aggregate_for_variable_by_list_nuts.R

Defines functions aggregate_for_variable_by_list_nuts

Documented in aggregate_for_variable_by_list_nuts

#' @title Data aggregation function for a variable and a list of regions containing NUTS IDS
#' @description Aggregates data for a variable and a list containing lists with nuts_ids to aggregate. Each object of the list is one region (aggregation unit) which has to contain NUTS IDs which will be aggregated. 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 nuts_list list, were each object contains nuts_ids. The data will be aggregated for the NUTS IDs in each object of the list and the object name of the list will be used for the regional ID of the aggregated observation.
#' @param group_by "sum", "mean", "median", indicates whether aggregation should work by using the sum, the mean or the median of the data.
#' @param NUTS_level indicates, which regional level should be used for aggregation. Default is "NUTS3".
#' @param data dataframe, from which you want to extract your data. Default is the EntrancesData dataset
#' @return dataframe
#' @export
#' @examples
#' #First generate a List with region objects containing NUTS IDs. The object name (e.g. Germany) will be used as variable Name for the aggregated variable.
#' #Then you can use the list to aggregate the values of a variable for the NUTS IDs in each list object.
#' #Aggregation can work either by sum, mean or median.
#' #Additionally you can filter your NUTS IDs for a specific level (NUTS3, NUTS2, NUTS1, NUTS country).
#' #You can define a data frame, if you don't want to use the EntrancesData data set from the package.
#'
#' list_with_NUTS<-list(Germany=c("DED01", "DED02"), Italy=c("ITG2c", "ITG2B"))
#' aggregate_for_variable_by_list_nuts(VarName="pop", nuts_list=list_with_NUTS, group_by="sum", data=EntrancesData, NUTS_level="NUTS3")
aggregate_for_variable_by_list_nuts<- function(VarName, nuts_list, group_by="sum", data=EntrancesData, NUTS_level="NUTS3"){
  dataout<-EntrancesData[1,]
  dataout<-dataout[-1,]
  cnames<-colnames(dataout)
  for (unit in 1:length(nuts_list)){
    reg_name<-names(nuts_list)[unit]
    dataout<-rbind(dataout, aggregate_for_variable_by_nuts(nuts_ids=unlist(nuts_list[unit]), VarName=VarName, aggregation_name = paste("aggr", VarName, reg_name, 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.