R/split_colnames.R

Defines functions split_colnames

Documented in split_colnames

#' Tools for working with column names
#'
#' @description Splits column names into two rows. The first row are the new
#'              column names, the second are the first row of the data.
#'
#' @param df A data frame.
#' @param sep Separator between column names and first row.
#' @export
split_colnames <- function(df, sep = "_"){

  df_names <- names(df) %>%
    stringr::str_split(sep, simplify = T)

  df_names <- rlang::set_names(df_names[,2], df_names[,1])

  df <- magrittr::set_colnames(df, names(df_names))

  out <- rbind(df_names, df)

  return(out)

}
EstherHerbert/Useful.functions documentation built on Jan. 31, 2025, 10:41 a.m.