R/dog_spread_SKS.R

Defines functions dog_spread_SKS

Documented in dog_spread_SKS

#' Collect SKS in One Cell
#'
#' This function takes a data frame in long format
#'
#' @param df A data frame in long format.
#' @param Billing_code The variable to spread.
#' @param Accession_number The ID for the Billing code.
#' @param col_name Choose the output column name.
#' @param ... Other
#'
#' @importFrom rlang .data
#'
#' @return A data frame with a new column which we have chosen with \code{col}.
#' @export
#' @examples
#' dog_spread_SKS(ris_test, col_name = SKS)
dog_spread_SKS <- function(df, Billing_code = Billing_code, Accession_number = Accession_number, col_name, ...){

  Billing_code <- rlang::enquo(Billing_code)
  Accession_number <- rlang::enquo(Accession_number)
  col_name <- rlang::enquo(col_name)
  name2 <- rlang::quo_name(col_name)

out <-   df %>%
    distinct(!!Billing_code, !!Accession_number) %>%
  # distinct not important, removes duplicate rows, mostly if the same accession number has two blanks
    group_by(!!Accession_number) %>%
    arrange(!!Billing_code, .by_group=TRUE) %>%
    mutate(s3 = LETTERS[row_number()] ) %>%
    ungroup() %>%
    tidyr::spread(.data$s3, !!Billing_code) %>%
    replace(is.na(.), "") %>%
    tidyr::unite(2:ncol(.), col= !!col_name,  sep="/") %>%
    mutate(!!name2 := stringr::str_remove(!!col_name, "/+$"))

  out
}
# testdf_real %>% group_by_all() %>% filter(n()>1) %>% ungroup() # number of dublicate rows...
davidbaniadam/rispacs documentation built on Nov. 4, 2019, 9:43 a.m.