R/dataframe_formatting_functions.R

Defines functions left_join_list rowToColNames

Documented in left_join_list rowToColNames

#' A function to join a list of dataframes together around common columns
#'
#' Join a list of dataframes to a specified dataframe in the list, all the rows of which will be preserved
#' @param list a list of data.frames
#' @param by the column to join on (can be a characteer vector of col names c('col1', 'col2',...))
#' @param base_df_index the position in the list of the dataframe to join to
#' @examples
#' left_join_list()

left_join_list <- function(list, by, base_df_index = 1){


  base <- list[[base_df_index]]
  list <- list[seq(length(list)) %>% .[.!=base_df_index]]

  for(i in 1:length(list)){


      base %<>%
        dplyr::left_join(list[[i]], by = by)

    }


  return(base)

}

#' A function to set the values in a row of a data frame to be its column names
#'
#' Set the values in a row of a data frame to be its column names
#' @param df a data.frame
#' @param row the row you want to use as column names
#' @param cleanNames use the cleanNames fun ction to clean the names?
#' @examples
#' rowToColNames()

rowToColNames <- function(df, row = 1, cleanNames = T){
  df %<>%
    stats::setNames(.[row,]) %>%
    .[(row+1):nrow(.),]

  if(cleanNames == T){
    df %<>%
      stats::setNames(cleanNames(names(.)) %>% tolower)
  }

  return(df)
}
BillyEhrenberg/FTutilityfuncs documentation built on March 5, 2020, 12:42 a.m.