This function creates a labelled flat table or flat proportional (marginal) table.

flat_table(
  data,
  ...,
  margin = c("counts", "cell", "row", "col"),
  digits = 2,
  show.values = FALSE,
  weights = NULL
)

Arguments

data

A data frame. May also be a grouped data frame (see 'Note' and 'Examples').

...

One or more variables of data that should be printed as table.

margin

Specify the table margin that should be computed for proportional tables. By default, counts are printed. Use margin = "cell", margin = "col" or margin = "row" to print cell, column or row percentages of the table margins.

digits

Numeric; for proportional tables, digits indicates the number of decimal places.

show.values

Logical, if TRUE, value labels are prefixed by the associated value.

weights

Bare name, or name as string, of a variable in x that indicates the vector of weights, which will be applied to weight all observations. Default is NULL, so no weights are used.

Value

An object of class ftable.

Note

data may also be a grouped data frame (see group_by) with up to two grouping variables. Cross tables are created for each subgroup then.

See also

frq for simple frequency table of labelled vectors.

Examples

data(efc)

# flat table with counts
flat_table(efc, e42dep, c172code, e16sex)
#>                                                      e16sex male female
#> e42dep               c172code                                          
#> independent          low level of education                    5      7
#>                      intermediate level of education          15     30
#>                      high level of education                   1      4
#> slightly dependent   low level of education                   16     25
#>                      intermediate level of education          39     96
#>                      high level of education                  13     26
#> moderately dependent low level of education                   28     33
#>                      intermediate level of education          59    104
#>                      high level of education                  18     44
#> severely dependent   low level of education                   31     34
#>                      intermediate level of education          43    120
#>                      high level of education                  11     39

# flat table with proportions
flat_table(efc, e42dep, c172code, e16sex, margin = "row")
#>                                                      e16sex  male female
#> e42dep               c172code                                           
#> independent          low level of education                 41.67  58.33
#>                      intermediate level of education        33.33  66.67
#>                      high level of education                20.00  80.00
#> slightly dependent   low level of education                 39.02  60.98
#>                      intermediate level of education        28.89  71.11
#>                      high level of education                33.33  66.67
#> moderately dependent low level of education                 45.90  54.10
#>                      intermediate level of education        36.20  63.80
#>                      high level of education                29.03  70.97
#> severely dependent   low level of education                 47.69  52.31
#>                      intermediate level of education        26.38  73.62
#>                      high level of education                22.00  78.00

# flat table from grouped data frame. You need to select
# the grouping variables and at least two more variables for
# cross tabulation.
library(dplyr)
efc %>%
  group_by(e16sex) %>%
  select(e16sex, c172code, e42dep) %>%
  flat_table()
#> 
#> Grouped by: male
#> 
#>                                 e42dep independent slightly dependent moderately dependent severely dependent
#> c172code                                                                                                     
#> low level of education                           5                 16                   28                 31
#> intermediate level of education                 15                 39                   59                 43
#> high level of education                          1                 13                   18                 11
#> 
#> 
#> Grouped by: female
#> 
#>                                 e42dep independent slightly dependent moderately dependent severely dependent
#> c172code                                                                                                     
#> low level of education                           7                 25                   33                 34
#> intermediate level of education                 30                 96                  104                120
#> high level of education                          4                 26                   44                 39
#> 

efc %>%
  group_by(e16sex, e42dep) %>%
  select(e16sex, e42dep, c172code, n4pstu) %>%
  flat_table()
#> 
#> Grouped by: male, independent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                             4            1            0            0             0
#> intermediate level of education                    7            2            4            2             0
#> high level of education                            1            0            0            0             0
#> 
#> 
#> Grouped by: male, slightly dependent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                             9            3            1            2             0
#> intermediate level of education                   22            7            8            2             0
#> high level of education                            8            1            2            2             0
#> 
#> 
#> Grouped by: male, moderately dependent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                            15            6            4            2             0
#> intermediate level of education                   27           14           13            4             1
#> high level of education                           11            2            5            0             0
#> 
#> 
#> Grouped by: male, severely dependent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                             8            5           10            7             0
#> intermediate level of education                    7            3           12           20             1
#> high level of education                            4            2            3            2             0
#> 
#> 
#> Grouped by: female, independent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                             3            1            2            1             0
#> intermediate level of education                   19            6            5            0             0
#> high level of education                            2            2            0            0             0
#> 
#> 
#> Grouped by: female, slightly dependent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                            17            4            2            1             0
#> intermediate level of education                   63           13           14            4             1
#> high level of education                           18            3            4            1             0
#> 
#> 
#> Grouped by: female, moderately dependent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                            16           11            5            1             0
#> intermediate level of education                   44           36           17            7             0
#> high level of education                           22           12            8            2             0
#> 
#> 
#> Grouped by: female, severely dependent
#> 
#>                                 n4pstu No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
#> c172code                                                                                                 
#> low level of education                            14            4            8            7             0
#> intermediate level of education                   39           18           35           23             2
#> high level of education                            7            8           16            8             0
#> 

# now it gets weird...
efc %>%
  group_by(e16sex, e42dep) %>%
  select(e16sex, e42dep, c172code, n4pstu, c161sex) %>%
  flat_table()
#> 
#> Grouped by: male, independent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            1      3
#>                                 Care Level 1             0      1
#>                                 Care Level 2             0      0
#>                                 Care Level 3             0      0
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            3      4
#>                                 Care Level 1             0      2
#>                                 Care Level 2             1      3
#>                                 Care Level 3             2      0
#>                                 Care Level 3+            0      0
#> high level of education         No Care Level            0      1
#>                                 Care Level 1             0      0
#>                                 Care Level 2             0      0
#>                                 Care Level 3             0      0
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: male, slightly dependent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            3      6
#>                                 Care Level 1             1      1
#>                                 Care Level 2             1      0
#>                                 Care Level 3             0      2
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            7     15
#>                                 Care Level 1             2      5
#>                                 Care Level 2             1      7
#>                                 Care Level 3             2      0
#>                                 Care Level 3+            0      0
#> high level of education         No Care Level            1      7
#>                                 Care Level 1             0      1
#>                                 Care Level 2             0      2
#>                                 Care Level 3             1      1
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: male, moderately dependent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            7      8
#>                                 Care Level 1             0      6
#>                                 Care Level 2             0      4
#>                                 Care Level 3             0      2
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            3     24
#>                                 Care Level 1             1     13
#>                                 Care Level 2             4      9
#>                                 Care Level 3             1      3
#>                                 Care Level 3+            1      0
#> high level of education         No Care Level            5      6
#>                                 Care Level 1             2      0
#>                                 Care Level 2             1      4
#>                                 Care Level 3             0      0
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: male, severely dependent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            0      8
#>                                 Care Level 1             1      4
#>                                 Care Level 2             1      9
#>                                 Care Level 3             0      7
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            1      6
#>                                 Care Level 1             0      3
#>                                 Care Level 2             2     10
#>                                 Care Level 3             1     19
#>                                 Care Level 3+            0      1
#> high level of education         No Care Level            1      3
#>                                 Care Level 1             2      0
#>                                 Care Level 2             0      3
#>                                 Care Level 3             0      2
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: female, independent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            1      2
#>                                 Care Level 1             0      1
#>                                 Care Level 2             0      2
#>                                 Care Level 3             1      0
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            4     15
#>                                 Care Level 1             2      4
#>                                 Care Level 2             2      3
#>                                 Care Level 3             0      0
#>                                 Care Level 3+            0      0
#> high level of education         No Care Level            0      2
#>                                 Care Level 1             0      2
#>                                 Care Level 2             0      0
#>                                 Care Level 3             0      0
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: female, slightly dependent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            3     14
#>                                 Care Level 1             0      4
#>                                 Care Level 2             1      1
#>                                 Care Level 3             0      1
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level           12     51
#>                                 Care Level 1             3     10
#>                                 Care Level 2             3     11
#>                                 Care Level 3             1      3
#>                                 Care Level 3+            1      0
#> high level of education         No Care Level            6     12
#>                                 Care Level 1             0      3
#>                                 Care Level 2             1      3
#>                                 Care Level 3             1      0
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: female, moderately dependent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            6     10
#>                                 Care Level 1             2      9
#>                                 Care Level 2             1      4
#>                                 Care Level 3             0      1
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            8     36
#>                                 Care Level 1            11     25
#>                                 Care Level 2             8      9
#>                                 Care Level 3             0      7
#>                                 Care Level 3+            0      0
#> high level of education         No Care Level            7     15
#>                                 Care Level 1             4      8
#>                                 Care Level 2             2      6
#>                                 Care Level 3             0      2
#>                                 Care Level 3+            0      0
#> 
#> 
#> Grouped by: female, severely dependent
#> 
#>                                               c161sex Male Female
#> c172code                        n4pstu                           
#> low level of education          No Care Level            4     10
#>                                 Care Level 1             2      2
#>                                 Care Level 2             2      6
#>                                 Care Level 3             2      5
#>                                 Care Level 3+            0      0
#> intermediate level of education No Care Level            6     33
#>                                 Care Level 1             3     15
#>                                 Care Level 2             5     30
#>                                 Care Level 3            11     12
#>                                 Care Level 3+            1      1
#> high level of education         No Care Level            1      6
#>                                 Care Level 1             2      6
#>                                 Care Level 2             7      9
#>                                 Care Level 3             3      5
#>                                 Care Level 3+            0      0
#>