R/prism_output.R

Defines functions prism_output

Documented in prism_output

#' prism_output
#' @export prism_output
#'
prism_output <- function(tranformed_data) {
  tmp <- tranformed_data %>%
    pivot_wider(
      id_cols = c(Type, Treatment, SubjectID, Dose),
      names_from = Time,
      values_from = Response_Transformed
    ) %>%
    tail(22)

  max_n <- tmp %>%
    group_by(Type, Treatment, Dose) %>%
    summarize(n = n()) %>%
    ungroup() %>%
    slice_max(n, with_ties = FALSE) %>%
    select(n) %>%
    unlist()


  prism <- map_df(.x = unique(tmp$Treatment), .f = ~ {
    out <- tmp %>%
      filter(Treatment == .x) %>%
      mutate(SubjectID = paste("SubjectID", row_number()))

    if (nrow(out) < max_n) {
      diff <- max_n - nrow(out)
      trt_add <- distinct(.data = out, Type, Treatment, Dose)
      for (i in 1:diff) {
        out <- out %>%
          bind_rows(data.frame(trt_add,
            SubjectID = paste("SubjectID", i + nrow(out))
          ))
      }
    }
    return(out)
  }) %>%
    as.matrix() %>%
    t()

  rownames(prism)[2] <- ""
  rownames(prism)[1] <- "Timepoint"
  return(prism)
}
fdrennan/test documentation built on April 23, 2022, 12:37 a.m.