-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mround() and round_and_suppress()
- Loading branch information
1 parent
49dc167
commit 07762b8
Showing
5 changed files
with
84 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
) | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.