Skip to content

Commit

Permalink
Add mround() and round_and_suppress()
Browse files Browse the repository at this point in the history
  • Loading branch information
izaak-jephson committed Jul 23, 2024
1 parent 49dc167 commit 07762b8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 28 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export(make_all_supp_negative)
export(make_number_percent)
export(make_string_numeric)
export(make_supp_negative)
export(mround)
export(round_and_suppress)
export(transpose_data)
importFrom(magrittr,"%>%")
importFrom(rlang,":=")
Expand Down
46 changes: 46 additions & 0 deletions R/maths_tools.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Rounds to the nearest desired multiple.
#'
#' This function behaves like Excel's `mround`.
#' @param x Number to be rounded
#' @param multiple Multiple to be rounded to
#' @export

mround <- function(x, multiple){
multiple * janitor::round_half_up(x / multiple)
}

#' Rounds whole data frame to the nearest multiple and suppresses any values below 5 a limit
#'
#' This function take a data frame, rounds every value to the nearest given multiple,
#' then converts any values that round to zero to a given suppression value.
#' @param data A data frame to be rounded and suppressed
#' @param multiple Multiple to which to round table
#' @param suppression_value Value to convert suppressed values to. Defaults to -1
#' for ease of output to Excel.
#' @export

round_and_suppress <- function(data, multiple = 5, suppression_value = -1){

data %>%
dplyr::mutate(
dplyr::across(
tidyselect::contains("Percentage"),
~ {janitor::round_half_up(., 2)
}
)
) %>%
dplyr::mutate(
dplyr::across(
tidyselect::where(is.numeric) &
!tidyselect::contains("Percentage") &
!tidyselect::contains("Median"),
~ {dplyr::case_when(
. < 1 ~ janitor::round_half_up(., 2),
. == 0 ~ 0,
sssstats::mround(., multiple) == 0 ~ suppression_value,
TRUE ~ sssstats::mround(., multiple)
)
}
)
)
}
28 changes: 0 additions & 28 deletions README.Rmd

This file was deleted.

16 changes: 16 additions & 0 deletions man/mround.Rd

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

20 changes: 20 additions & 0 deletions man/round_and_suppress.Rd

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

0 comments on commit 07762b8

Please sign in to comment.