prettyunits
prettyunits copied to clipboard
Pad p-values with space at beginning so decimal aligns
When using the function pretty_p_value(), I'd like the resulting character to be aligned at the decimal point for nicer usage when printing tibbles or using gt() or kable() to print tables. Example of what I'd like this to look like (not implementation) is displayed in ps_f_alt below:
# remotes::install_github("r-lib/prettyunits")
library(prettyunits)
library(tibble)
ps <- c(1, 0, NA, 0.01, 0.0000001)
ps_format <- pretty_p_value(ps, minval = .05)
ps_format_2 <- stringr::str_pad(ps_format, 5)
pdf <- data.frame(ps=ps, ps_f=ps_format, ps_f_alt=ps_format_2)
ptib <- as_tibble(pdf)
pdf
#> ps ps_f ps_f_alt
#> 1 1e+00 1.00 1.00
#> 2 0e+00 <0.05 <0.05
#> 3 NA <NA> <NA>
#> 4 1e-02 <0.05 <0.05
#> 5 1e-07 <0.05 <0.05
ptib
#> # A tibble: 5 × 3
#> ps ps_f ps_f_alt
#> <dbl> <chr> <chr>
#> 1 1 1.00 " 1.00"
#> 2 0 <0.05 "<0.05"
#> 3 NA <NA> <NA>
#> 4 0.01 <0.05 "<0.05"
#> 5 0.0000001 <0.05 "<0.05"
Created on 2024-03-20 with reprex v2.0.2