Formats p-values according to common publication standards, with configurable thresholds and decimal places.
Details
The function applies the following rules:
P-values below
thresholdare formatted as "< 0.001" (or HTML equivalent)P-values >=
thresholdare rounded todigitsdecimal placesNAvalues are preserved asNA_character_Values > 1 or < 0 return
NA_character_with a warning
Examples
# Basic usage
format_pvalue(0.0234)
#> [1] "0.023"
#> "0.023"
format_pvalue(0.00005)
#> [1] "< 0.001"
#> "< 0.001"
# Vector input
pvals <- c(0.5, 0.05, 0.001, 0.0001, NA)
format_pvalue(pvals)
#> [1] "0.500" "0.050" "0.001" "< 0.001" NA
#> "0.500" "0.050" "0.001" "< 0.001" NA
# Custom threshold
format_pvalue(0.005, threshold = 0.01)
#> [1] "< 0.01"
#> "< 0.01"
# HTML output
format_pvalue(0.0001, html = TRUE)
#> [1] "< 0.001"
#> "< 0.001"