Skip to content

Commit

Permalink
Adding a decimals argument to get_means and dunnettt
Browse files Browse the repository at this point in the history
  • Loading branch information
JDenn0514 committed Nov 4, 2024
1 parent 09d7449 commit 2e19bb3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
38 changes: 27 additions & 11 deletions R/dunnett.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#' of each level. Default is `FALSE`
#' @param show_diffs Logical. Determines if the output should contain the
#' difference in means
#' @param decimals Number of decimals each number should be rounded to. Default is 3.
#' @param na.rm Logical. Determines if NAs should be removed
#'
#' @examples
Expand Down Expand Up @@ -128,6 +129,7 @@ dunnett.data.frame <- function(
conf.level = 0.95,
show_means = FALSE,
show_diffs = TRUE,
decimals = 3,
na.rm = TRUE
) {

Expand Down Expand Up @@ -188,7 +190,8 @@ dunnett.data.frame <- function(
control = {{ control }},
conf.level = {{ conf.level }},
show_means = show_means,
show_diffs = show_diffs
show_diffs = show_diffs,
decimals = decimals
),
.options = furrr::furrr_options(seed = NULL)
)
Expand Down Expand Up @@ -259,7 +262,8 @@ dunnett.data.frame <- function(
control = {{ control }},
conf.level = {{ conf.level }},
show_means = show_means,
show_diffs = show_diffs
show_diffs = show_diffs,
decimals = decimals
)

}
Expand Down Expand Up @@ -334,6 +338,7 @@ dunnett.grouped_df <- function(
conf.level = 0.95,
show_means = FALSE,
show_diffs = TRUE,
decimals = 3,
na.rm = TRUE
) {

Expand Down Expand Up @@ -375,7 +380,8 @@ dunnett.grouped_df <- function(
control = control,
conf.level = conf.level,
show_means = show_means,
show_diffs = show_diffs
show_diffs = show_diffs,
decimals = decimals
),
.options = furrr::furrr_options(seed = NULL)
)
Expand Down Expand Up @@ -474,7 +480,8 @@ dunnett_helper <- function(
control = NULL,
conf.level = 0.95,
show_means = FALSE,
show_diffs = TRUE
show_diffs = TRUE,
decimals = 3
) {

if (isFALSE(show_means) && isFALSE(show_diffs)) {
Expand Down Expand Up @@ -608,23 +615,32 @@ dunnett_helper <- function(
# add the stars based on p-value
stars = stars_pval(p.value)
) %>%
# round all numeric variables to the third decimal
# round all numeric variables
dplyr::mutate(
dplyr::across(
dplyr::where(is.numeric),
~round(.x, 3)
~round(.x, decimals)
)
)

if (isTRUE(show_means)) {
### calculate the means if show_means is TRUE

# calculate the means
means <- data %>%
# group the data by treats
dplyr::group_by(.data[[treats]]) %>%
if (is.null(wt)) {
# calculate the means
get_means({{ x }})
means <- data %>%
# group the data by treats
dplyr::group_by(.data[[treats]]) %>%
# calculate the means
get_means({{ x }}, decimals = decimals)
} else {
# calculate the means
means <- data %>%
# group the data by treats
dplyr::group_by(.data[[treats]]) %>%
# calculate the means
get_means({{ x }}, wt = {{ wt }}, decimals = decimals)
}

control_df <- data.frame(
# the value in the treatments column is set to whatever the control is
Expand Down
9 changes: 5 additions & 4 deletions R/get_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#' means to be grouped by.
#' @param wt Weights. Add if you have a weighting variable and want to get
#' weighted means
#' @param decimals Number of decimals to round the results to. Default is 3.
#'
#' @examples
#' # load the package
Expand Down Expand Up @@ -55,7 +56,7 @@
#'
#'

get_means <- function(data, x, group, wt) {
get_means <- function(data, x, group, wt, decimals = 3) {

# get the object's name
x_lab <- deparse(substitute(x))
Expand Down Expand Up @@ -156,8 +157,8 @@ get_means <- function(data, x, group, wt) {
conf.low = mean - qt(1 - ((1 - 0.95) / 2), n - 1) * std.error,
# calculate the higher CI
conf.high = mean + qt(1 - ((1 - 0.95) / 2), n - 1) * std.error,
# round all of the numbers to the second decimal
dplyr::across(dplyr::where(is.numeric), ~round(.x, 2))
# round all of the numbers
dplyr::across(dplyr::where(is.numeric), ~round(.x, decimals))
) %>%
dplyr::select(-std.error)

Expand Down Expand Up @@ -212,7 +213,7 @@ get_means <- function(data, x, group, wt) {
# calculate the higher CI
conf.high = mean + qt(1 - ((1 - 0.95) / 2), n - 1) * std.error,
# round all of the numbers to the second decimal
dplyr::across(dplyr::where(is.numeric), ~round(.x, 2))
dplyr::across(dplyr::where(is.numeric), ~round(.x, decimals))
) %>%
dplyr::select(-std.error)

Expand Down
2 changes: 2 additions & 0 deletions man/dunnett.Rd

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

5 changes: 4 additions & 1 deletion man/dunnett_helper.Rd

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

4 changes: 3 additions & 1 deletion man/get_means.Rd

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

0 comments on commit 2e19bb3

Please sign in to comment.