R/df_combine.R

Defines functions df_combine

#' Create new data frame with predefined data
#'
#' This function combines data from an existing data file and extends with further
#' information based upon specifications given in a seperate file.
#' 
#' The new data frame have to combine data from data frame 1 (df1) and data frame 2
#' (df2), which is not included in both files. The data can be variable or constant. 
#' The targeted data is predefined in df2 and can be accessed by the optional input 
#' parameters. 
#'
#'
#' @param df1 A data frame
#' @param df2 A data frame
#' @param s1 An optional string, default = 'fields'
#' @param s2 An optional string, default = 'value'
#' @param s3 An optional string, default = 'type'
#' @param s4 An optional string, default = 'variable'
#' @param s5 An optional string, default = 'constant'
#' @return A data frame defined by df2 and contaning data from df1 and df2.
#' 
#' @author J. Hopp, C. Sahin
#' @note Version 0, Creation 16.10.2019
#' 
#' @examples
#' dim(df1) = 100000 30
#' dim(df2) = 10 3
#' df3 <- df_combine(df1, df2)
#' dim(df3) = 100000 10
#' @export
#' 

df_combine <- function(.df, .df_input, 
                      .field_name = 'fields', .value_name = 'value', .type_name = 'type',
                      .type_variable = 'variable', .type_const = 'constant'){
  
  # get data frames with constant and variable values --------------------------
  df_input_constant <- get(.df_input)
  df_input_variable <- get(.df)
  
  # selecting target and default field names and required data type ------------
  out_variable_value <- df_input_constant  %>%  
                          filter(!!rlang::sym(.type_name) == !!.type_variable) %>%
                          pull(!!.value_name ) 
  out_variable_fields <- df_input_constant %>%  
                          filter(!!rlang::sym(.type_name) == !!.type_variable) %>%
                          pull(!!.field_name) 
  out_constant_value  <- df_input_constant  %>%  
                          filter(!!rlang::sym(.type_name) == !!.type_const) %>%
                          pull(!!.value_name )  
  out_constant_fields <- df_input_constant  %>%  
                          filter(!!rlang::sym(.type_name) == !!.type_const) %>%
                          pull(!!.field_name)

  # creating a data frame with appropriate dimension and names -----------------
  
  df_dummy <- data.frame(matrix(NA, 
                                nrow = dim(df_input_variable)[1], 
                                ncol = dim(df_input_constant)[1]))
  names(df_dummy) <- c(df_input_constant %>% pull(!!.field_name))
  
  # change the values of the data frame by means of column names
  # if the values are static/constant, then replicate the rows
  # if the values are variable replace by original data frame values
  df_dummy[out_constant_fields] <- rep(out_constant_value, each=NROW(df_dummy[out_constant_fields])) 
  df_dummy[out_variable_fields] <- df_input_variable[out_variable_value]


  return (df_dummy)
}
irisweyermenkhoff/toyota-idv-functions documentation built on March 4, 2020, 9:57 a.m.