Format p-values for use in R Markdown documents

This function formats p-values for use in R Markdown documents to accomplish the following three formatting goals for APA style:

  • Round to three digits
  • Trim leading zero
  • Display $p < .001$ in instances where a p-value is rounded down to zero
f_pvalue = function(p.value, symbol = "="){
  p.value <- round(p.value, digits = 3)
  if (p.value == 0) {
    return("p < .001")
  } else {
    return(paste0("p", symbol, round(p.value, digits = 3)))
  }
}

Also see these other solutions:

Avatar
Chad Michael Eyer
Graduate Student

Student. Thinker. Learner. Leader. Human.