#' A function to clean names to remove funny characters from strings
#'
#' Swaps whitespaces for underscores, retains underscores, removes special/accented characters
#' @param string a string
#' @export
#' @examples
#' cleanNames()
cleanNames <- function(string){
string %<>% gsub('\\s','AWHITESPACE',.) %>% gsub('\\_','ANUNDERSCORE',.) %>% iconv(.,to='ASCII//translit') %>%
gsub('[^[:alnum:]]','',.) %>% gsub('AWHITESPACE|ANUNDERSCORE','_',.) %>%
tolower
}
#' A function to clean column names in a df
#'
#'Swaps whitespaces for underscores, retains underscores, removes special/accented characters
#' @param df a df or tibble
#' @export
#' @examples
#' clean_col_Names()
clean_col_Names <- function(df){
names(df) = tolower(cleanNames(names(df) %>% stringr::str_replace('/','_')))
df
}
#' A function to merge multi-row column headers into one row
#'
#' Choose a first and last row, all rows between will be merged
#' @param df a data.frame or tibble
#' @param first_row the first row to merge
#' @param last_row the last row to merge
#' @param fill_gaps_in_rows indices of rows with missing values that you want filled.
#' @export
#' @examples
#' merge_col_headers()
merge_col_headers <- function(df, first_row, last_row, fill_gaps_in_rows = NULL){
if(!is.null(fill_gaps_in_rows)){
df <- fill_gaps_in_rows %>%
purrr::map_dfr(~{
if(is.na(df[1,1])){
df[1,1] = '||holding_value||'
}
df[1,] = df[1,] %>%
as.character %>%
zoo::na.locf(.) %>%
ifelse(. == '||holding_value||',
NA,
.)
df
})
}
headers <- df %>%
.[first_row:last_row,]
new_names <- names(headers) %>%
purrr::map_chr(~{
pull(headers,.x) %>%
.[!is.na(.)] %>%
myCollapse('_') %>%
cleanNames()
})
df %>%
.[(last_row+1):nrow(.),] %>%
purrr::set_names(new_names)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.