user14079890
user14079890

Reputation:

manage decimal values in output summary

sample data:

dataa<-data.frame(
  aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,2,4,4,4,1,2,5,3,5),
col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,3,2,7,7,2,3,7,2,4)
)

sum<- expss::cro_cpct(dat2[[1]],dat2[[2]])

Upvotes: 0

Views: 145

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388907

You can try this function :

library(expss)
library(flextable)
library(dplyr)
library(rlang)

tab_std_cross <- function(dat1,var1,grouping_var){
  
  var1 <- parse_expr(var1)
  var2 <- parse_expr(grouping_var)
  
  dat2 <- dat1 %>% select(!!var1,!!var2)
  var_lab(dat2[[1]]) <- ""
  var_lab(dat2[[2]]) <- ""
  tab1 <- cro_cpct(dat2[[1]],dat2[[2]]) 
  tab1 <- as.data.frame(tab1)
  tab1[which(tab1[,1]=="#Total cases"),1] <- "N"
  tab1[is.na(tab1)] <- 0
  tab1 <- tab1 %>%
    mutate(
      across(
        .cols = where(is.numeric),
        .fns = ~ round(.x, digits = 1)
      )
    )
  tab1[tab1 == 0] <- '--' # --- (1)
  mask_indices <- sapply(tab1, function(x) x[length(x)] %in% c(3, 4, 5)) %>%
    which()  
  tab1[, mask_indices] <- "--"
  tab1[-nrow(tab1), -c(1, mask_indices)] <- sapply(
      tab1[-nrow(tab1),-c(1, mask_indices)], function(x) # --- (2)
    ifelse(x != '--', paste(format(as.numeric(x), nsmall = 1), "%"), x)) # --- (3)
  tab1 <- flextable(tab1)
  tab1
}

Things changed in the function :

  1. Replace all 0 values with "--"
  2. Removed column 1 from turning into "%" since it does not make sense to add "%" sign to row labels.
  3. Used ifelse to avoid turning "--" to "-- %". Showed numbers only till 1 decimal place.
tab_std_cross (dat1 = dataa,var1 = "col1",grouping_var="col2")

enter image description here

Upvotes: 1

Related Questions