It performs a univariate statistical analysis of a dataset by group and formats the results so that they can be used with the tabulator() function.

summarizor(x, by = character(), overall_label = NULL)

Arguments

x

dataset

by

columns names to be used as grouping columns

overall_label

label to use as overall label

Note

This is very first version of the function; be aware it can evolve or change.

Illustrations

Examples

z <- summarizor(CO2[-c(1, 4)],
  by = "Treatment",
  overall_label = "Overall"
)
ft_1 <- as_flextable(z)
ft_1
#> a flextable object.
#> col_keys: `variable`, `stat`, `dummy1`, `nonchilled@blah`, `dummy2`, `chilled@blah`, `dummy3`, `Overall@blah` 
#> header has 1 row(s) 
#> body has 24 row(s) 
#> original dataset sample: 
#>   variable stat cts@nonchilled cts@chilled cts@Overall percent@nonchilled
#> 1    Plant  Qn1              5           0           5              0.125
#> 2    Plant  Qn2              7           0           7              0.175
#> 3    Plant  Qn3              7           0           7              0.175
#> 4    Plant  Qc1              0           7           7              0.000
#> 5    Plant  Qc3              0           7           7              0.000
#>   percent@chilled percent@Overall data_type@nonchilled data_type@chilled
#> 1       0.0000000      0.06097561             discrete          discrete
#> 2       0.0000000      0.08536585             discrete          discrete
#> 3       0.0000000      0.08536585             discrete          discrete
#> 4       0.1666667      0.08536585             discrete          discrete
#> 5       0.1666667      0.08536585             discrete          discrete
#>   data_type@Overall value1@nonchilled value1@chilled value1@Overall
#> 1          discrete                NA             NA             NA
#> 2          discrete                NA             NA             NA
#> 3          discrete                NA             NA             NA
#> 4          discrete                NA             NA             NA
#> 5          discrete                NA             NA             NA
#>   value2@nonchilled value2@chilled value2@Overall blah@nonchilled blah@chilled
#> 1                NA             NA             NA                             
#> 2                NA             NA             NA                             
#> 3                NA             NA             NA                             
#> 4                NA             NA             NA                             
#> 5                NA             NA             NA                             
#>   blah@Overall dummy1 nonchilled@blah dummy2 chilled@blah dummy3 Overall@blah
#> 1                                                                            
#> 2                                                                            
#> 3                                                                            
#> 4                                                                            
#> 5                                                                            

# version 2 with your own functions ----
n_format <- function(n, percent) {
  z <- character(length = length(n))
  wcts <- !is.na(n)
  z[wcts] <- sprintf("%.0f (%.01f %%)",
    n[wcts], percent[wcts] * 100)
  z
}

stat_format <- function(stat, num1, num2,
                        num1_mask = "%.01f",
                        num2_mask = "(%.01f)") {
  z_num <- character(length = length(num1))

  is_mean_sd <- !is.na(num1) & !is.na(num2) & stat %in% "mean_sd"
  is_median_iqr <- !is.na(num1) & !is.na(num2) &
    stat %in% "median_iqr"
  is_range <- !is.na(num1) & !is.na(num2) & stat %in% "range"
  is_num_1 <- !is.na(num1) & is.na(num2)

  z_num[is_num_1] <- sprintf(num1_mask, num1[is_num_1])

  z_num[is_mean_sd] <- paste0(
    sprintf(num1_mask, num1[is_mean_sd]),
    " ",
    sprintf(num2_mask, num2[is_mean_sd])
  )
  z_num[is_median_iqr] <- paste0(
    sprintf(num1_mask, num1[is_median_iqr]),
    " ",
    sprintf(num2_mask, num2[is_median_iqr])
  )
  z_num[is_range] <- paste0(
    "[",
    sprintf(num1_mask, num1[is_range]),
    " - ",
    sprintf(num1_mask, num2[is_range]),
    "]"
  )

  z_num
}

tab_2 <- tabulator(z,
  rows = c("variable", "stat"),
  columns = "Treatment",
  `Est.` = as_paragraph(
    as_chunk(stat_format(stat, value1, value2))),
  `N` = as_paragraph(as_chunk(n_format(cts, percent)))
)

ft_2 <- as_flextable(tab_2, separate_with = "variable")
ft_2
#> a flextable object.
#> col_keys: `variable`, `stat`, `dummy1`, `@nonchilled@Est.`, `@nonchilled@N`, `dummy2`, `@chilled@Est.`, `@chilled@N`, `dummy3`, `@Overall@Est.`, `@Overall@N` 
#> header has 2 row(s) 
#> body has 24 row(s) 
#> original dataset sample: 
#>   variable stat cts@nonchilled cts@chilled cts@Overall percent@nonchilled
#> 1    Plant  Qn1              5           0           5              0.125
#> 2    Plant  Qn2              7           0           7              0.175
#> 3    Plant  Qn3              7           0           7              0.175
#> 4    Plant  Qc1              0           7           7              0.000
#> 5    Plant  Qc3              0           7           7              0.000
#>   percent@chilled percent@Overall data_type@nonchilled data_type@chilled
#> 1       0.0000000      0.06097561             discrete          discrete
#> 2       0.0000000      0.08536585             discrete          discrete
#> 3       0.0000000      0.08536585             discrete          discrete
#> 4       0.1666667      0.08536585             discrete          discrete
#> 5       0.1666667      0.08536585             discrete          discrete
#>   data_type@Overall value1@nonchilled value1@chilled value1@Overall
#> 1          discrete                NA             NA             NA
#> 2          discrete                NA             NA             NA
#> 3          discrete                NA             NA             NA
#> 4          discrete                NA             NA             NA
#> 5          discrete                NA             NA             NA
#>   value2@nonchilled value2@chilled value2@Overall Est.@nonchilled Est.@chilled
#> 1                NA             NA             NA                             
#> 2                NA             NA             NA                             
#> 3                NA             NA             NA                             
#> 4                NA             NA             NA                             
#> 5                NA             NA             NA                             
#>   Est.@Overall N@nonchilled N@chilled N@Overall dummy1 @nonchilled@Est.
#> 1                                                                      
#> 2                                                                      
#> 3                                                                      
#> 4                                                                      
#> 5                                                                      
#>   @nonchilled@N dummy2 @chilled@Est. @chilled@N dummy3 @Overall@Est. @Overall@N
#> 1                                                                              
#> 2                                                                              
#> 3                                                                              
#> 4                                                                              
#> 5