R/pval_to_jama.R

Defines functions pval_to_jama

Documented in pval_to_jama

#' Convert p values to JAMA format
#'
#' Converts a vector of p values into character vector that reflect
#' JAMA journal standards for the reporting of p values.
#'
#' @param p Vector of p values.
#' @param includePequals Include "p = .XX"?
#' @export
#' @examples
#' pval_to_jama(c(.50, .0004, .000000001, .098, .044, .048, 0.052))
#**********************************************************
pval_to_jama <- function(p, includePequals = FALSE){

  out <- dplyr::case_when(p < .001 ~ "<.001",
                          p < .01 ~ sprintf("%0.3f", p),
                          p >= .01 ~ sprintf("%0.2f", p))

  indices.05 <- which(out == .05)

  out[indices.05] <- sprintf("%0.3f", p[indices.05])

  out <- stringr::str_replace(out, "^0\\.", ".")

  if (includePequals == TRUE){

    out <- dplyr::case_when(stringr::str_detect(out, "<") ~ paste0("p", out),
                            TRUE ~ paste0("p=", out))

  }

  out

}
#**********************************************************
wepelham3/sack2 documentation built on Aug. 5, 2023, 5:53 a.m.