Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created December 15, 2016 08:18
Show Gist options
  • Select an option

  • Save jrnold/64e69c2f5767b7ea94ebae658951f1a0 to your computer and use it in GitHub Desktop.

Select an option

Save jrnold/64e69c2f5767b7ea94ebae658951f1a0 to your computer and use it in GitHub Desktop.
p-value formatting
library(stringr)
library(dplyr)
drop_leading0 <- function(x) {
str_replace(x, "\\b0+\\.(?=[0-9])", ".")
}
format_pval <- function(x, eps = 0.001, digits = 2, drop = TRUE) {
ret <- if_else(
x < eps,
str_c("<",as.character(eps)),
formatC(x, digits = digits, format = "f")
)
if (drop) {
ret <- drop_leading0(ret)
}
ret
}
@jrnold

jrnold commented Dec 15, 2016

Copy link
Copy Markdown
Author

Simple function to format p-values (truncate at a lower value, and drop leading 0's).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment