Round and format p-values. Can also mark significant p-values with stars.

p_round(x, ..., digits = 3)

p_format(
  x,
  ...,
  new.col = FALSE,
  digits = 2,
  accuracy = 1e-04,
  decimal.mark = ".",
  leading.zero = TRUE,
  trailing.zero = FALSE,
  add.p = FALSE,
  space = FALSE
)

p_mark_significant(
  x,
  ...,
  new.col = FALSE,
  cutpoints = c(0, 1e-04, 0.001, 0.01, 0.05, 1),
  symbols = c("****", "***", "**", "*", "")
)

p_detect(data, type = c("all", "p", "p.adj"))

p_names()

p_adj_names()

Arguments

x

a numeric vector of p-values or a data frame containing a p value column. If data frame, the p-value column(s) will be automatically detected. Known p-value column names can be obtained using the functions p_names() and p_adj_names()

...

column names to manipulate in the case where x is a data frame. P value columns are automatically detected if not specified.

digits

the number of significant digits to be used.

new.col

logical, used only when x is a data frame. If TRUE, add a new column to hold the results. The new column name is created by adding, to the p column, the suffix "format" (for p_format()), "signif" (for p_mak_significant()).

accuracy

number to round to, that is the threshold value above wich the function will replace the pvalue by "<0.0xxx".

decimal.mark

the character to be used to indicate the numeric decimal point.

leading.zero

logical. If FALSE, remove the leading zero.

trailing.zero

logical. If FALSE (default), remove the training extra zero.

add.p

logical value. If TRUE, add "p=" before the value.

space

logical. If TRUE (default) use space as separator between different elements and symbols.

cutpoints

numeric vector used for intervals

symbols

character vector, one shorter than cutpoints, used as significance symbols.

data

a data frame

type

the type of p-value to detect. Can be one of c("all", "p", "p.adj").

Value

a vector or a data frame containing the rounded/formatted p-values.

Functions

  • p_round(): round p-values

  • p_format(): format p-values. Add a symbol "<" for small p-values.

  • p_mark_significant(): mark p-values with significance levels

  • p_detect(): detects and returns p-value column names in a data frame.

  • p_names(): returns known p-value column names

  • p_adj_names(): returns known adjust p-value column names

Examples


# Round and format a vector of p-values
# ::::::::::::::::::::::::::::::::::::::::::::
# Format
p <- c(0.5678, 0.127, 0.045, 0.011, 0.009, 0.00002, NA)
p_format(p)
#> [1] "0.568"   "0.127"   "0.045"   "0.011"   "0.009"   "<0.0001" "NA"     

# Specify the accuracy
p_format(p, accuracy = 0.01)
#> [1] "0.568" "0.127" "0.045" "0.011" "<0.01" "<0.01" "NA"   

# Add p and remove the leading zero
p_format(p, add.p = TRUE, leading.zero = FALSE)
#> [1] "p=.568"  "p=.127"  "p=.045"  "p=.011"  "p=.009"  "p<.0001" "p=NA"   

# Remove space before and after "=" or "<".
p_format(p, add.p = TRUE, leading.zero = FALSE, space = FALSE)
#> [1] "p=.568"  "p=.127"  "p=.045"  "p=.011"  "p=.009"  "p<.0001" "p=NA"   

# Mark significant p-values
# ::::::::::::::::::::::::::::::::::::::::::::
p_mark_significant(p)
#> [1] "0.5678"    "0.127"     "0.045*"    "0.011*"    "0.009**"   "2e-05****"
#> [7] "NA"       

# Round, the mark significant
p %>% p_round(digits = 2) %>% p_mark_significant()
#> [1] "0.57"      "0.13"      "0.04*"     "0.01**"    "0.009**"   "2e-05****"
#> [7] "NA"       

# Format, then mark significant
p %>% p_format(digits = 2) %>% p_mark_significant()
#> [1] "0.568"       "0.127"       "0.045*"      "0.011*"      "0.009**"    
#> [6] "<0.0001****" "NA"         

# Perform stat test, format p and mark significant
# ::::::::::::::::::::::::::::::::::::::::::::
ToothGrowth %>%
  group_by(dose) %>%
  t_test(len ~ supp) %>%
  p_format(digits = 2, leading.zero = FALSE) %>%
  p_mark_significant()
#> # A tibble: 3 × 9
#>    dose .y.   group1 group2    n1    n2 statistic    df p      
#> * <dbl> <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl> <chr>  
#> 1   0.5 len   OJ     VC        10    10    3.17    15.0 .0064**
#> 2   1   len   OJ     VC        10    10    4.03    15.4 .001***
#> 3   2   len   OJ     VC        10    10   -0.0461  14.0 .964