I want to add one more parameter in my function which could check and get the stats i want to show in table.
for example if i want to show all stats or average only or i want to show percentile25 or percentile75
result <- fun1(df, var = "hp", Banner = banner1,stats ="all")
result <- fun1(df, var = "hp", Banner = banner1,stats ="Percentile25")
result <- fun1(df, var = "hp", Banner = banner1,stats =c("median","Percentile25"))
library(expss)
library(dplyr)
library(rlang)
df <- mtcars
df <- head(df,3)
var <- "hp"
df$vs <- 1
banner1 <- list(df$vs)
fun1 <- function(data, var, Banner,p25 = getOption("percentile25", default = "25th percentile"),
perc_75 = getOption("percentile75", default = "75th percentile")) {
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))
np25 <- "something else"
flist <- list2(
!!np25 := perc_25,
"Mean" = Mean,
"Median" = Median,
"75th Perc" = perc_75,
"Valid N" = valid_n
)
if (stats != "all") {
stats <- tolower(stats)
flist <- flist [names(flist) %in% stats]
} else {
flist <- flist
}
cmb_fun <- do.call(combine_functions,flist)
t1 <- cross_fun(
data,
data[[var1]],
col_vars = Banner,
fun = cmb_fun
)
return(t1)
}
# Example usage with masking set to TRUE
result <- fun1(df, var = "hp", Banner = banner1,)
print(result)
also tried with map2, but not working any different idea..????