Skip to content

Commit

Permalink
a formatter for p-values
Browse files Browse the repository at this point in the history
cf. #144
  • Loading branch information
larmarange committed Jul 6, 2018
1 parent 8257643 commit 89a6cec
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -83,6 +83,8 @@ export(pretty_breaks)
export(probability_trans)
export(probit_trans)
export(pseudo_log_trans)
export(pvalue)
export(pvalue_format)
export(reciprocal_trans)
export(regular_minor_breaks)
export(rescale)
Expand Down
53 changes: 53 additions & 0 deletions R/formatter.r
Expand Up @@ -438,3 +438,56 @@ time_format <- function(format = "%H:%M:%S", tz = "UTC") {
}
}
}

#' p-values formatter
#'
#' Formatter for p-values, adding a symbol "<" for small p-values.
#'
#' @return `pvalue_format` returns a function with single parameter
#' `x`, a numeric vector, that returns a character vector.
#' @param accuracy Number to round to.
#' @param decimal.mark The character to be used to indicate the numeric
#' decimal point.
#' @param add_p Add "p=" before the value?
#' @param x A numeric vector of p-values.
#' @export
#' @examples
#' p <- c(.50, 0.12, .045, .011, .009, .00002, NA)
#' pvalue(p)
#' pvalue(p, accuracy = .01)
#' pvalue(p, add_p = TRUE)
#' custom_function <- pvalue_format(accuracy = .1, decimal.mark = ",")
#' custom_function(p)
pvalue_format <- function(accuracy = .001, decimal.mark = ".", add_p = FALSE) {
function(x) pvalue(
x,
accuracy = accuracy,
decimal.mark = decimal.mark,
add_p = add_p
)
}

#' @rdname pvalue_format
#' @export
pvalue <- function(x, accuracy = .001, decimal.mark = ".", add_p = FALSE) {
res <- number(
x,
accuracy = accuracy,
decimal.mark = decimal.mark,
big.mark = ""
)
if (add_p) res <- paste0("p=", res)
below <- number(
accuracy,
accuracy = accuracy,
decimal.mark = decimal.mark,
big.mark = ""
)
if (add_p) {
below <- paste0("p<", below)
} else {
below <- paste0("<", below)
}
res[x < accuracy] <- below
res
}
36 changes: 36 additions & 0 deletions man/pvalue_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tests/testthat/test-formatter.r
Expand Up @@ -149,3 +149,25 @@ test_that("negative comes before prefix", {
test_that("missing values preserved", {
expect_equal(dollar(NA_real_), "$NA")
})


# p-value formatter --------------------------------------------------------

test_that("pvalue formatter work", {
expect_equal(
pvalue(c(.5, .045, .0002, NA)),
c("0.500", "0.045", "<0.001", "NA")
)
expect_equal(
pvalue(c(.5, .045, .0002, NA), accuracy = .01),
c("0.50", "0.04", "<0.01", "NA")
)
expect_equal(
pvalue(c(.5, .045, .0002, NA), decimal.mark = ","),
c("0,500", "0,045", "<0,001", "NA")
)
expect_equal(
pvalue(c(.5, .045, .0002, NA), add_p = TRUE),
c("p=0.500", "p=0.045", "p<0.001", "p=NA")
)
})

0 comments on commit 89a6cec

Please sign in to comment.