R/add_variable.R

Defines functions add_variable

#' Adds a variable based on a computation from already present data
#'
#' This function adds a variable based on a computation using data already
#' present. Unit will be autogenerated by combining the old units according to
#' the formula, which might result in correct, but ugly units.
#' ATTENTION: Current (June 2016) version assumes that all variables are from
#' the same source_id and model. Therefore, no computation of e.g energy
#' intensity by combining GDP data from WDI and energy data from IEA is
#' possible.
#'
#' @param df A compatible dataframe
#' @param equation A formula like GDPpC ~ GDP / Population
#' @param append Shall the result be appended to the original dataframe (default: TRUE)
#' @param ... further arguments passed on to \code{\link[dplyr]{filter}}
#' @return A compatible dataframe
#' @import dplyr reshape2
#' @export
add_variable <- function(df, equation, append = TRUE, ...){
  # sanity check: Currently, this function supports only computations based on
  # the same source_id and model
  if(dim(unique(df[c("source_id", "model")]))[1] != 1){
    warning("Currently only computations based on one source_id and model are reliable!")
  }
  if(append){
    df_orig <- df
  }

  df <- filter(df, ...)

  # create appropirate subset
  df <- filter(df, variable %in% all.vars(equation))

  # split up equation into rhs and lhs
  equation_lhs <- strsplit(as.character(equation), split = " ~ ")[[1]][1]
  equation_rhs <- strsplit(as.character(equation), split = " ~ ")[[1]][2]
  vars_rhs <- all.vars(equation)[2:length(all.vars(equation))]
  operators <- gsub("([A-z, 0-9])", " ", equation_rhs)

  orig_vars <- unique(df$variable)

  df_units <- unique(df[c("variable", "unit")])
  df_units <- mutate(df_units, variable = as.character(variable),
                               unit = as.character(unit))

  unit <- equation_rhs

  for(var in unique(df_units$variable)){
    unit <- sub(var, as.character(df_units[df_units$variable == var, "unit"]), unit)
  }

  df <- dcast(df, source_id + model + scenario + spatial + temporal ~ variable)

  # df <- mutate_(df, rhs(equation))
  df <- mutate_(df, equation_rhs)

  # the last column is the one with the result, need to rename it
  colnames(df)[ncol(df)] <- equation_lhs

  df <- melt(df, id.vars = c("source_id", "model", "scenario", "spatial", "temporal"))

  df <- filter(df, !variable%in%orig_vars)

  df <- filter(df, !is.na(value))

  df <- mutate(df, unit = unit)

  if(append){
    df <- rbind(df_orig, df)
  }

  return(df)
}
nroming/IDA documentation built on March 6, 2020, 2:23 a.m.