#' Separate a collapsed column record into multiple rows.
#'
#' Wrapper around `tidyr::separate_rows()`. Performs the operation for a given
#' list of dividers (delimiters).
#' Iterates through the set of possible dividers.
#'
#' If a feature/variable in a record contains multiple delimited values, this
#' separates the values, placing each on in its own row (creating a new record).
#' The transformation is similar to wide to long, but only (feature, record)
#' having delimited values will create new rows.
#'
#' @param df A `DataFrame`.
#' @param column The name of the column/feature to perfom the separation on.
#' @param dividers A vector of delimiters to
#' @param trim_ws A boolean; determines if trailing white spaces (potentially
#' generated by the split), are trimmed or not.
#'
#' @return A `DataFrame` with the additional rows (as many as performed splits).
#' @export
#'
#' @examples
#' melt_rows(
#' businesses[4:8, 3:4],
#' "trading_name",
#' dividers = c("\\|", " trading as ", "t/a", "\\/")
#' )
#'
#' @importFrom stringr str_c str_squish
#' @importFrom tidyr separate_rows
#' @importFrom dplyr distinct mutate_at filter
#' @importFrom purrr when
#' @importFrom rlang sym
melt_rows <- function(df, column, dividers = c("\\|"), trim_ws = TRUE){
dividers_ <- str_c(dividers, collapse = "|")
# quo_column <- sym(column)
df %>%
separate_rows(column, sep = dividers_) %>%
when(trim_ws ~ mutate_at(., column, str_trim),
~.
) %>%
# FIXME will also filter out pre-existing NA in the column
# filter((!! quo_column) != "") %>%
distinct()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.