This function formats p values for presentation. P values >= 0.05 are rounded to a specified number of digits, while p values < 0.05 are displayed as "<.05", "<.01", or "<.001".

nice_p(p, equal_sign = FALSE, digits = 3, stars = FALSE)

Arguments

p

Vector of p values. NA values are allowed and will be returned as NA.

equal_sign

If TRUE an equal sign is set before p values. Default is FALSE.

digits

Number of digits to show for p values >= 0.05. Default is 3.

stars

If TRUE, significance stars are added for p values < 0.05. Default is FALSE.

Value

A character vector with nicely formatted p values suitable for presentation.

Details

If stars is TRUE, significance stars are added for p values < 0.05. The function also allows to set an equal sign before p values if equal_sign is TRUE.

P values are formatted as follows:

  • p >= 0.05: Rounded to the specified number of digits (e.g., 0.123)

  • 0.01 <= p < 0.05: Displayed as "<.05" (or with stars if stars is TRUE)

  • 0.001 <= p < 0.01: Displayed as "<.01" (or with stars if stars is TRUE)

  • p < 0.001: Displayed as "<.001" (or with stars if stars is TRUE)

Author

Juergen Wilbert

Examples

p <- c(0.04, 0.9, 0.10, 0.001, 1, NA)
nice_p(p)
#> [1] "<.05"  ".900"  ".100"  "<.01"  "1.000" NA     
paste0("p", nice_p(p, equal_sign = TRUE))
#> [1] "p<.05"   "p=.900"  "p=.100"  "p<.01"   "p=1.000" "pNA"    
nice_p(p, stars = TRUE)
#> [1] ".040*"  ".900"   ".100"   ".001**" "1.000"  NA      
paste0("p", nice_p(p, equal_sign = TRUE, stars = TRUE))
#> [1] "p=.040*"  "p=.900"   "p=.100"   "p=.001**" "p=1.000"  "pNA"     
nice_p(c(0.0004, 0.005, 0.03, 0.2), stars = TRUE)
#> [1] "<.001***" ".005**"   ".030*"    ".200"    
paste0("p", nice_p(c(0.0004, 0.005, 0.03, 0.2), equal_sign = TRUE, stars = TRUE))
#> [1] "p<.001***" "p=.005**"  "p=.030*"   "p=.200"