Changing the name of statistics dynamically

This the the below example i am trying and before creating cmb_fun , i want to change the label of stats (percentile 25,percentile 75, mean, median) from global option dynamically.

fun1 <- function(data, var, Banner,p25 = getOption("percentile25", default = "25th percentile"),
                 perc_75 = getOption("percentile75", default = "75th percentile"),
                 Mean = getOption("Meann", default = "Mean"),
                 Median = getOption("Mediann", default = "Median")
                 stats="all") {
  var1 <- rlang::parse_expr(var)
  
  perc_25 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2]
  perc_75 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[4]
  Mean <- function(x) mean(x, na.rm = TRUE)
  Median <- function(x) median(x, na.rm = TRUE)
  valid_n <- function(x) sum(!is.na(x))
  
  flist <- list2(
    "Percentile 25"= perc_25,
    "Mean" = Mean,
    "Median" = Median,
    "75th Perc" = perc_75,
    "Valid N" = valid_n
  )
  
  stat_names <- names(flist) %>% tolower()
  if (stats != "all") {
    stats <- tolower(stats)
    flist <- flist [stat_names %in% stats]
  } else {
    flist <- flist 
  }
  
  flist1 <- list2(
    !!p25 := perc_25,
    "Valid N" = valid_n
  )
 
  cmb_fun <- do.call(combine_functions,flist1)
  t1 <- cross_fun(
    data,
    data[[var1]],
    col_vars = Banner,
    fun = cmb_fun
  )
  
  
  return(t1)
}

debugonce(fun1)
result <- fun1(df, var = "hp", Banner = banner1,stats="percentile 25")
print(result)

after the line flist <- flist i want to change the percentile25 = "Perc_25" dynamically i want to make it dynamic like whatever i will select in stats , it should apply for all selected stats like

result <- fun1(df, var = "hp", Banner = banner1,stats=c("percentile 25"))
result <- fun1(df, var = "hp", Banner = banner1,stats="all")
result <- fun1(df, var = "hp", Banner = banner1,stats=c("percentile 25","percentile 75")

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.