Skip to contents

Formats p-values according to major scientific publication standards (APA, AMA, NEJM, Lancet, etc.) or custom user-defined settings. This function provides flexible control over decimal places, leading zeros, and threshold notation for very small p-values.

Usage

format_p_value(
  p,
  style = "default",
  digits = NULL,
  leading.zero = NULL,
  min.threshold = NULL,
  decimal.mark = NULL,
  use.scientific = NULL
)

Arguments

p

Numeric vector of p-values to format.

style

Character string specifying the formatting style. One of:

  • "default": Current behavior with scientific notation (backward compatible)

  • "apa": APA Style - no leading zero, 3 decimals, "< .001" threshold

  • "nejm": NEJM Style - leading zero, 3 decimals, "< 0.001" threshold

  • "lancet": Lancet Style - leading zero, 4 decimals, "< 0.0001" threshold

  • "ama": AMA Style - no leading zero, 3 decimals, "< .001" threshold

  • "graphpad": GraphPad Style - leading zero, 4 decimals, "< 0.0001" threshold

  • "scientific": Scientific notation for genomics/GWAS studies

digits

Integer specifying the number of decimal places. If provided, overrides the style default.

leading.zero

Logical indicating whether to include leading zero before decimal point (e.g., "0.05" vs ".05"). If provided, overrides the style default.

min.threshold

Numeric specifying the minimum p-value to display exactly. Values below this threshold are shown as "< threshold" (e.g., "< 0.001"). If NULL, the selected style's default threshold is used; styles without a threshold show exact values. If provided, overrides the style default. Must be a single positive finite number.

decimal.mark

Character string to use as the decimal mark. If NULL, uses getOption("OutDec").

use.scientific

Logical indicating whether to force scientific notation. If NULL, uses the style default.

Value

Character vector of formatted p-values.

Details

P-value formatting conventions vary across scientific disciplines and journals:

APA Style (Psychology, Social Sciences):

  • No leading zero (write ".05" not "0.05")

  • 3 decimal places

  • Report as "p < .001" for very small values

NEJM/Medical Journals:

  • Leading zero (write "0.05" not ".05")

  • 3 decimal places

  • P < 0.001: report as "< 0.001"

Scientific Notation (GWAS, Genomics):

  • Used when very small p-values are meaningful (e.g., 5e-8 threshold)

  • Appropriate for high-dimensional data analyses

Examples

# Test p-values
p_vals <- c(0.76404, 0.0432, 0.0043, 0.00018, 1.7e-11)

# Different styles
format_p_value(p_vals, style = "default")
#> [1] "0.76404" "0.04320" "0.00430" "0.00018" "1.7e-11"
format_p_value(p_vals, style = "apa")
#> [1] ".764"   ".043"   ".004"   "< .001" "< .001"
format_p_value(p_vals, style = "nejm")
#> [1] "0.764"   "0.043"   "0.004"   "< 0.001" "< 0.001"
format_p_value(p_vals, style = "lancet")
#> [1] "0.7640"   "0.0432"   "0.0043"   "0.0002"   "< 0.0001"

# Custom formatting
format_p_value(p_vals, digits = 2, leading.zero = FALSE, min.threshold = 0.01)
#> [1] ".76404" ".04320" "< .01"  "< .01"  "< .01" 

# Override style defaults
format_p_value(p_vals, style = "nejm", digits = 4)
#> [1] "0.7640"   "0.0432"   "0.0043"   "< 0.0010" "< 0.0010"