Skip to contents
library(procR)

# Read dataset
head(eusilc)
#>       hid        region hsize eqsize eqIncome pid      id age gender ecoStat
#> 39993   1 Upper Austria     2    1.5 11128.45   1 0000101  25   male       1
#> 39994   1 Upper Austria     2    1.5 11128.45   2 0000102  24 female       4
#> 31004   2        Styria     2    1.5 19694.85   1 0000201  57 female       1
#> 31005   2        Styria     2    1.5 19694.85   2 0000202  53   male       1
#> 29071   3        Styria     1    1.0  5066.24   1 0000301  30 female       6
#> 41322   4 Upper Austria     3    1.8 31480.01   1 0000401  32   male       1
#>       citizenship   py010n   py050n  py090n py100n py110n py120n py130n py140n
#> 39993       Other 16692.67     0.00    0.00      0      0      0      0      0
#> 39994          AT     0.00     0.00    0.00      0      0      0      0      0
#> 31004          AT     0.00 12564.59    0.00      0      0      0      0      0
#> 31005          AT 16884.06     0.00    0.00      0      0      0      0      0
#> 29071          AT     0.00     0.00 5066.24      0      0      0      0      0
#> 41322          AT 25047.39     0.00    0.00      0      0      0      0      0
#>       hy040n  hy050n hy070n hy080n hy090n  hy110n hy130n  hy145n  main
#> 39993      0    0.00      0      0   0.00    0.00   0.00    0.00  TRUE
#> 39994      0    0.00      0      0   0.00    0.00   0.00    0.00 FALSE
#> 31004      0    0.00      0      0   0.00    0.00  93.63 -187.26 FALSE
#> 31005      0    0.00      0      0   0.00    0.00  93.63 -187.26  TRUE
#> 29071      0    0.00      0      0   0.00    0.00   0.00    0.00  TRUE
#> 41322      0 7167.39      0      0  31.15 1349.91   0.00    0.00  TRUE

# Add weights
eusilc$weight <- rlnorm(nrow(eusilc), meanlog = 0, sdlog = 2)

# Normalise to total population size
eusilc$weight <- eusilc$weight / sum(eusilc$weight) * 8269

Overview

This vignette demonstrates the features included in procR by way of a small, reproducible example. The following steps are performed:

  • Define variable-formats
  • Perform aggregation
  • Export to Excel

Define variable-formats

A variable in the dataset might contain many values. Which of these values should be shown in the aggregated table, how they should be grouped and in what order they should be displayed is determined using so-called ‘variable-formats’. The idea is identical to the PROC FORMAT command in SAS. Consider in particular the format age_f.

region_f <- new_format_asis(eusilc, var = region, name = "Region", add_total = TRUE)

citizenship_f <- new_format_asis(eusilc, var = citizenship, name = "Citizenship", add_total = TRUE, include_na = FALSE)

gender_f <- new_format_asis(eusilc, var = gender, name = "Gender", add_total = TRUE)

ecostat_f <- new_format(var = ecoStat, name = "Economic Status", add_total = TRUE,
                        "Working",
                        davon(1 ~ "Working full time",
                              2 ~ "Working part time"),
                        "Not working",
                        davon(3 ~ "Unemployed",
                              4 ~ "Pupil, student, further training or unpaid work experience or in compulsory military/community service",
                              5 ~ "In retirement/early retirement",
                              6 ~ "Permanently disabled/unfit to work/other inactive person",
                              7 ~ "Fulfilling domestic tasks"))

# Inspect some of the varformats
gender_f
#> Gender      code                                                                
#> ╲╴Total     gender %in% structure(1L, levels = c("male", "female"), class = ... 
#>    ├╴male   gender %in% structure(1L, levels = c("male", "female"), class = ... 
#>    └╴female gender %in% structure(2L, levels = c("male", "female"), class = ... 
#> 
ecostat_f
#> Economic Status                                      code                       
#> ╲╴Total                                              ecoStat %in% 1 | ecoSta... 
#>    ├╴Working                                         ecoStat %in% 1 | ecoSta... 
#>    │  ├╴Working full time                            ecoStat %in% 1             
#>    │  └╴Working part time                            ecoStat %in% 2             
#>    └╴Not working                                     ecoStat %in% 3 | ecoSta... 
#>       ├╴Unemployed                                   ecoStat %in% 3             
#>       ├╴Pupil, student, further training or unpaid work e... ecoStat %in% 4             
#>       ├╴In retirement/early retirement               ecoStat %in% 5             
#>       ├╴Permanently disabled/unfit to work/other inactive... ecoStat %in% 6             
#>       └╴Fulfilling domestic tasks                    ecoStat %in% 7             

Perform aggregation

The desired table is specified as a formula, where the left-hand-side describes the row variables and the right-hand-side the column variables. Two operators can be used:

  • + means that variable-formats should be displayed one after the other, without interaction
  • * means that variable-formats should be cross-combined, such that each value in one format is combined with each value in the other format

The parameter where is a filter condition applied before performing the aggregation. w is a variable containing weights. Both parameters should be provided as unquoted expressions.


system.time({
  tab <- create_table_count(df = eusilc,
                            formula = citizenship_f * ecostat_f ~ region_f * gender_f,
                            where = main == TRUE,
                            w = weight) |>
    set_metadata(title = "Population in Austria by citizenship, economic status, region and gender",
                 subtitle = "Estimates (in thousands)",
                 footer = "Source: Austrian EU-SILC data from 2006")
})
#>    user  system elapsed 
#>   1.053   0.200   1.294

to_gt(tab)
Population in Austria by citizenship, economic status, region and gender
Estimates (in thousands)
Citizenship, Economic Status
Total
Burgenland
Lower Austria
Vienna
Carinthia
Styria
Upper Austria
Salzburg
Tyrol
Vorarlberg
Total male female Total male female Total male female Total male female Total male female Total male female Total male female Total male female Total male female Total male female
Total Total 3 459  2 106  1 352  98  67  31  612  386  227  821  451  371  250  179  70  458  279  178  555  385  170  258  140  118  278  139  139  129  80  49 
   Working 2 009  1 415    594  43  34   9  337  249   87  508  316  192  133   98  34  253  175   78  357  285   71  144   94   50  146   98   48   90  64  25 
      Working full time 1 809  1 353    457  42  34   7  314  247   67  463  295  168  123   97  26  224  167   57  318  269   48  122   93   29  125   91   34   80  59  20 
      Working part time   200     62    138   1   0   1   23    3   21   45   21   24   10    1   9   28    8   20   39   16   23   22    1   21   21    7   14   10   5   5 
   Not working 1 450    691    758  55  33  22  275  136  139  313  134  179  117   81  36  205  105  100  199  100   99  113   45   68  133   42   91   39  16  24 
      Unemployed   119     79     40   2   2   0   14   11    3   65   46   19   10    3   7    6    4    2   12    9    4    4    2    2    4    0    4    2   2   0 
      Pupil, student, further training or unpaid work experience or in compulsory military/community service    44     13     31   0   0   0    0    0    0   32    5   26    2    1   0    5    3    2    2    1    1    3    2    2    0    0    0    0   0   0 
      In retirement/early retirement 1 172    571    602  49  28  22  237  123  114  197   81  115  100   74  26  164   83   81  166   86   80   98   42   56  126   40   86   35  13  22 
      Permanently disabled/unfit to work/other inactive person    52     27     25   3   3   0   13    1   11    5    1    4    2    2   0   15   13    2    9    4    5    3    0    3    1    1    0    1   1   0 
      Fulfilling domestic tasks    63      2     61   0   0   0   11    0   11   15    1   15    3    0   3   15    1   14   10    0   10    5    0    5    1    0    1    2   0   2 
   AT Total 3 257  1 990  1 267  95  65  31  597  373  224  723  403  320  236  167  69  439  267  172  537  372  165  242  132  109  262  132  129  126  78  48 
   Working 1 893  1 334    559  42  34   9  324  239   85  457  282  175  131   98  34  236  164   73  346  276   70  137   88   48  131   91   40   88  63  25 
      Working full time 1 709  1 277    433  41  34   7  302  237   66  419  263  155  122   97  25  212  156   56  308  260   48  115   87   28  112   85   27   78  58  20 
      Working part time   183     57    126   1   0   1   22    2   20   38   18   20    9    1   8   24    7   17   38   16   22   22    1   20   19    6   13   10   5   5 
   Not working 1 364    657    708  53  31  22  272  134  138  266  121  145  105   70  35  203  104   99  191   96   95  105   44   61  131   41   89   38  15  23 
      Unemployed    96     64     32   2   2   0   13   10    3   47   35   13   10    3   6    5    4    2   10    7    4    3    1    2    4    0    4    2   2   0 
      Pupil, student, further training or unpaid work experience or in compulsory military/community service    33     12     20   0   0   0    0    0    0   21    5   16    2    1   0    5    3    2    2    1    1    3    2    2    0    0    0    0   0   0 
      In retirement/early retirement 1 139    553    587  48  26  22  236  122  114  188   81  107   89   63  26  162   82   80  163   85   78   96   41   55  125   40   84   34  13  21 
      Permanently disabled/unfit to work/other inactive person    44     26     18   3   3   0   13    1   11    1    1    0    2    2   0   15   13    2    7    3    4    1    0    0    1    1    0    1   1   0 
      Fulfilling domestic tasks    52      2     51   0   0   0   11    0   10   10    1    9    3    0   3   15    1   14    9    0    9    2    0    2    1    0    1    1   0   1 
   EU Total    88     41     47   2   2   0    2    2    1   47   13   34   12   11   1    8    5    2    5    2    3    3    1    2    8    4    4    1   1   0 
   Working    39     24     15   0   0   0    2    1    1   20   11   10    0    0   0    6    5    1    1    1    0    2    1    1    6    4    2    0   0   0 
      Working full time    32     21     11   0   0   0    1    1    0   17    9    8    0    0   0    6    5    1    1    1    0    2    1    1    5    4    1    0   0   0 
      Working part time     6      3      4   0   0   0    1    0    0    4    2    2    0    0   0    0    0    0    0    0    0    0    0    0    1    0    1    0   0   0 
   Not working    49     18     32   2   2   0    1    0    0   27    3   24   12   11   1    2    1    1    4    1    3    1    0    1    2    0    1    0   0   0 
      Unemployed     4      3      2   0   0   0    0    0    0    3    2    1    1    0   0    0    0    0    0    0    0    0    0    0    0    0    0    0   0   0 
      Pupil, student, further training or unpaid work experience or in compulsory military/community service    10      0     10   0   0   0    0    0    0   10    0   10    0    0   0    0    0    0    0    0    0    0    0    0    0    0    0    0   0   0 
      In retirement/early retirement    24     15     10   2   2   0    0    0    0    4    0    4   11   11   0    2    0    1    2    1    2    1    0    1    2    0    1    0   0   0 
      Permanently disabled/unfit to work/other inactive person     5      0      4   0   0   0    0    0    0    4    0    4    0    0   0    0    0    0    1    0    1    0    0    0    0    0    0    0   0   0 
      Fulfilling domestic tasks     6      0      6   0   0   0    0    0    0    5    0    5    0    0   0    0    0    0    0    0    0    0    0    0    0    0    0    0   0   0 
   Other Total   113     74     39   1   1   0   13   11    2   51   34   17    1    0   1   11    7    4   13   12    1   13    6    7    9    3    6    2   1   1 
   Working    78     57     20   1   1   0   11    9    1   31   24    7    1    0   1   10    6    4    9    9    1    5    4    1    8    3    5    1   1   0 
      Working full time    67     55     12   1   1   0   10    9    1   28   23    5    1    0   0    6    6    1    9    9    0    5    4    0    7    2    5    1   1   0 
      Working part time    10      2      8   0   0   0    1    0    1    3    1    2    0    0   0    4    1    3    0    0    0    1    0    1    1    1    0    0   0   0 
   Not working    36     17     19   0   0   0    2    2    1   20   10   10    0    0   0    1    0    0    4    3    1    7    1    6    0    0    0    1   0   0 
      Unemployed    18     12      6   0   0   0    1    1    0   15    9    5    0    0   0    0    0    0    2    2    0    1    0    0    0    0    0    0   0   0 
      Pupil, student, further training or unpaid work experience or in compulsory military/community service     1      0      0   0   0   0    0    0    0    1    0    0    0    0   0    0    0    0    0    0    0    0    0    0    0    0    0    0   0   0 
      In retirement/early retirement     8      3      5   0   0   0    1    1    0    5    1    4    0    0   0    0    0    0    1    1    0    1    1    0    0    0    0    1   0   0 
      Permanently disabled/unfit to work/other inactive person     4      1      3   0   0   0    0    0    0    0    0    0    0    0   0    0    0    0    1    1    0    2    0    2    0    0    0    0   0   0 
      Fulfilling domestic tasks     4      0      4   0   0   0    0    0    0    0    0    0    0    0   0    0    0    0    0    0    0    3    0    3    0    0    0    0   0   0 
Source: Austrian EU-SILC data from 2006

Perform export


# Create empty workbook
wb <- openxlsx2::wb_workbook()

# Write data
wb <- wb_add_procr_table(wb, tab)

# Export
wb$save(file.path(tempdir(), "procR_demo.xlsx"))