R/limpiar_spaces.R

Defines functions limpiar_spaces

Documented in limpiar_spaces

#' Clean redundant spaces
#'
#' Remove excess spaces from the text variable.
#'
#' @param df Name of the Data Frame or Tibble object
#' @param text_var Name of the text variable/character vector
#' @return text variable/character vector with excess spaces removed
#' @export
#'
#' @examples
#' df <- data.frame(text_variable = "clean   the   spaces please")
#' limpiar_spaces(df, text_var = text_variable)
#'
#' limpiar_examples %>% dplyr::select(mention_content)
#'
#' limpiar_examples %>% limpiar_spaces() %>% dplyr::select(mention_content)
limpiar_spaces <- function(df, text_var = mention_content){

  df %>%
    dplyr::mutate({{ text_var }} := stringr::str_trim({{ text_var }}),
                  {{ text_var }} := stringr::str_squish({{ text_var }}),
                  {{ text_var }} := stringr::str_replace_all({{ text_var }}, "[:space:]+\\.", "."),
                  {{ text_var }} := stringr::str_replace_all({{ text_var }}, "[:space:]+\\,", ","),
                  {{ text_var }} := stringr::str_replace_all({{ text_var }}, "[:space:]+:", ":"),
                  {{ text_var }} := stringr::str_replace_all({{ text_var }}, "[:space:]+;", ","),
                  {{ text_var }} := stringr::str_replace_all({{ text_var }}, "[:space:]+\\!", "!"),
                  {{ text_var }} := stringr::str_replace_all({{ text_var }}, "[:space:]+\\?", "?"))
}
jpcompartir/LimpiaR documentation built on May 22, 2024, 4:19 a.m.