I am trying to create a sub function but I am unable to create which checks N in the table and then mask the values in tables.
df <- mtcars
df <- df[1:4,]
sub function
mask_func <- function(x,N,masking){
mask_2= c(5,7)
if(masking=="NA"){
ifelse (N < mask_2[2] & N >= mask_2[1] ,"--",
ifelse (N < mask_2[2] & N <= mask_2[1] ,"--",x))}
}
tab<-function(dataset, var, Name_of_variable, Pref,masking,...)
{
var <- rlang::parse_expr(var)
summ_tab1<- df %>% filter(!is.na(!!var)) %>% summarise(
q25 = format(round_half_up(quantile(!!var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2])),
Median = format(round_half_up(quantile(!!var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3])),
Mean = format(round_half_up( mean(!!var, na.rm=TRUE))),
q75 = format(round_half_up(quantile(!!var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[4])),
N = sum(!is.na(!!var)))
summ_tab<- summ_tab1 %>% mutate(" "= Name_of_variable,
q25 = ifelse(mask_func(q25,N,masking)=="--","--",paste0(Pref,mask_func(q25,N,masking))),
Median =ifelse(mask_func(Median,N,masking)=="--","--",paste0(Pref,mask_func(Median,N,masking))),
Mean = ifelse(mask_func(Mean,N,masking)=="--","--",paste0(Pref,mask_func(Mean,N,masking))),
q75 = ifelse(mask_func(q75,N,masking)=="--","--",paste0(Pref,mask_func(q75,N,masking))))
summ_tab
}
For example, if the input parameter is selected as masking="NA", then I am trying to create a sub function which checks the N for table summ_tab1, so that if N comes in mask_2= c(5,7). If N is less than 7, then it should mask values to "--" for q25 or q75. If N is less than 5, then it should mask values of median and mean to "--", which is the same as for masking="simple".
debug(tab)
tab(df, "cyl","col",masking="NA",Pref="$")
i want tables to mask like below