I am trying to hide values with "--" within my function but its not working. please help me what i am doing wrong.
so the condition is if mean and median is less than 3 then replace the values with "--"
same as for 25perc and 75 perc
library(expss)
library(dplyr)
df <- mtcars
df <- head(df,3)
var <- "hp"
df$vs <- 1
banner1 <- list(df$vs)
fun1 <- function(data, var, Banner, hide_val = FALSE) {
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_col(x, na.rm = TRUE)
Median <- function(x) median_col(x, na.rm = TRUE)
valid_n <- function(x) sum(!is.na(x))
t1 <- cross_fun(
data,
data[[var1]],
col_vars = Banner,
fun = combine_functions(
"25th Perc" = perc_25,
"Mean" = Mean,
"Median" = Median,
"75th Perc" = perc_75,
"Valid N" = valid_n
)
)
# Apply the masking conditions
t1 <- apply_labels(t1, "Mean" = ifelse(hide_val & t1[["Valid N"]] <= 3, "--", t1[["Mean"]]))
t1 <- apply_labels(t1, "Median" = ifelse(hide_val & t1[["Valid N"]] <= 3, "--", t1[["Median"]]))
t1 <- apply_labels(t1, "25th Perc" = ifelse(hide_val & t1[["Valid N"]] <= 4, "--", t1[["25th Perc"]]))
t1 <- apply_labels(t1, "75th Perc" = ifelse(hide_val & t1[["Valid N"]] <= 4, "--", t1[["75th Perc"]]))
return(t1)
}
# Example usage with masking set to TRUE
result <- fun1(df, var = "hp", Banner = banner1, hide_val = TRUE)
print(result)
the required output should be look like
1 | ||
---|---|---|
data[[var1]] | 25th Perc.25% | -- |
Mean | -- | |
Median | -- | |
75th Perc.75% | -- | |
Valid N | 3.0 |