Skip to content

Commit

Permalink
add decimal to percent argument to unite function
Browse files Browse the repository at this point in the history
  • Loading branch information
avallecam committed Sep 27, 2020
1 parent 7f805ea commit c177ec6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
26 changes: 18 additions & 8 deletions R/serosurvey_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param digits_dot digits of point estimate to round
#' @param digits_low digits of lower interval to round
#' @param digits_upp digits of upper interval to round
#' @param decimal_to_percent logical to multiply values by 100. TRUE by default.
#'
#' @export unite_dotwhiskers
#' @export serosvy_extract_posterior
Expand Down Expand Up @@ -44,20 +45,27 @@ unite_dotwhiskers <- function(data,
variable_upp,
digits_dot=3,
digits_low=2,
digits_upp=3) {
digits_upp=3,
decimal_to_percent=TRUE) {

# combo to maintain variable name in a new variable
c_var <- enquo(variable_dot)
c_var_name_01 <- c_var %>% rlang::as_name() %>% str_c("unite1_",.)
c_var_name_02 <- c_var %>% rlang::as_name() %>% str_c("unite2_",.)

data %>%
data_pre <- data %>%
mutate(estim_tab={{variable_dot}},
cilow_tab={{variable_low}},
ciupp_tab={{variable_upp}}) %>%
# from decimal to percentile
mutate_at(.vars = vars(estim_tab,cilow_tab,ciupp_tab),
.funs = funs(.*100)) %>%
ciupp_tab={{variable_upp}})

if (decimal_to_percent==TRUE) {
data_pre <- data_pre %>%
# from decimal to percentile
mutate_at(.vars = vars(estim_tab,cilow_tab,ciupp_tab),
.funs = funs(.*100))
}

data_out <- data_pre %>%
# digits must be value specific
mutate_at(.vars = vars(estim_tab),.funs = format,digits=digits_dot) %>%
mutate_at(.vars = vars(cilow_tab),.funs = format,digits=digits_low) %>%
Expand All @@ -67,10 +75,12 @@ unite_dotwhiskers <- function(data,
.funs = ~if_else(str_detect(.x,"NA"),NA_character_,.x)) %>%
# two proposal
mutate(
!!c_var_name_01 := str_c(estim_tab,"% (",cilow_tab," - ",ciupp_tab,")"),
!!c_var_name_02 := str_c(estim_tab,"%\n(",cilow_tab," - ",ciupp_tab,")")
!!c_var_name_01 := str_c(estim_tab," (",cilow_tab," - ",ciupp_tab,")"),
!!c_var_name_02 := str_c(estim_tab,"\n(",cilow_tab," - ",ciupp_tab,")")
) %>%
select(-estim_tab,-cilow_tab,-ciupp_tab)

data_out
}

#' @describeIn unite_dotwhiskers priorización con dos covariables
Expand Down
5 changes: 4 additions & 1 deletion man/unite_dotwhiskers.Rd

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

0 comments on commit c177ec6

Please sign in to comment.