Skip to contents

Formats p-values the way most journals expect: values above 0.9 (for digits = 1) are shown as ">0.9", values below 0.001 as "<0.001", and the number of decimals in between shrinks as the p-value grows.

Usage

style_pvalue(
  x,
  digits = 1,
  prepend_p = FALSE,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  na = NA_character_,
  ...
)

Arguments

x

(numeric)
P-values between 0 and 1. Values outside that range are returned as NA.

digits

(integer)
Number of decimal places for large p-values: 1, 2 or 3.

prepend_p

(scalar logical)
Whether to prefix the value with "p=" (or "p" when the value starts with < or >).

big.mark

(string)
Character used between every three digits to the left of the decimal mark.

decimal.mark

(string)
Character used as the decimal mark.

na

(string)
Replacement for missing values.

...

Not used. Accepted so that formatting functions can be called interchangeably.

Value

A character vector the same length as x.

Details

With digits = 1, values above 0.9 are shown as ">0.9"; a value gets 1 decimal place when it rounds to at least 0.2 at 1 decimal place, 2 when it rounds to at least 0.1 at 2 decimal places, and 3 down to 0.001; anything smaller is "<0.001". With digits = 2 the ceiling moves to ">0.99" and the 1-place branch drops out; with digits = 3 the ceiling is ">0.999" and every value from 0.001 to the ceiling gets 3 decimal places. Rounding is half away from zero throughout.

Examples

pvals <- c(
  1.5, 1, 0.999, 0.5, 0.25, 0.2, 0.197, 0.12, 0.10, 0.0999, 0.06,
  0.03, 0.002, 0.001, 0.00099, 0.0002, 0.00002, -1
)
style_pvalue(pvals)
#>  [1] NA       ">0.9"   ">0.9"   "0.5"    "0.3"    "0.2"    "0.2"    "0.12"  
#>  [9] "0.10"   "0.10"   "0.060"  "0.030"  "0.002"  "0.001"  "<0.001" "<0.001"
#> [17] "<0.001" NA      
style_pvalue(pvals, digits = 2, prepend_p = TRUE)
#>  [1] NA        "p>0.99"  "p>0.99"  "p=0.50"  "p=0.25"  "p=0.20"  "p=0.20" 
#>  [8] "p=0.12"  "p=0.10"  "p=0.10"  "p=0.060" "p=0.030" "p=0.002" "p=0.001"
#> [15] "p<0.001" "p<0.001" "p<0.001" NA       
style_pvalue(pvals, digits = 3)
#>  [1] NA       ">0.999" "0.999"  "0.500"  "0.250"  "0.200"  "0.197"  "0.120" 
#>  [9] "0.100"  "0.100"  "0.060"  "0.030"  "0.002"  "0.001"  "<0.001" "<0.001"
#> [17] "<0.001" NA