Debian Codegraph @

vignettes/gentab.Rmd

raw
1 ---
2 title: Generating Tables of Descriptive Statistics
3 output: rmarkdown::html_vignette
4 vignette: >
5 % \VignetteIndexEntry{Generating Tables of Descriptive Statistics}
6 % \VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r,echo=FALSE,message=FALSE}
11 knitr::opts_chunk$set(comment=NA,eval=FALSE)
12 ```
13
14
15 # Motivation
16
17 *R* is well suited for statistical graphics, the application of advanced data
18 analysis techniques, and Monte Carlo studies of estimators. However, it lacks
19 support for the typical data management tasks as they arise in the social
20 sciences as well as for the simple generation of desctiptive
21 statistics. "memisc" facilitates not only typical data management tasks of
22 survey researchers, but also the generation of descriptive statistics, as they
23 are often a first step in serious social science data analysis. In particular
24 it facilitates the creation of tables of percentages of other descriptive
25 statistics broken down by subgroups in the data. This is mainly achieved by the
26 function `genTable`, which is described in the following section. The section
27 thereafter describes how tables thus created can be exported to LaTeX and HTML.
28
29
30 Note that these examples require data not included in the package
31 (you need to register to [GESIS](https://www.gesis.org) to download the data).
32 The vignette code cannot be run without this additional data.
33
34 # Creating Tables of Descriptive Statistics
35
36 General table of descriptive statistics can be created using the function
37 `genTable()`. The syntax of calls to this function is quite similar to that of
38 the function `xtabs()`: The first argument (tagged `formula`) is a formula that
39 determines the descriptive statistics used and by what groups they are
40 computed. The left-hand side of the formula determines the statistics being
41 computed. The right-hand side determines the grouping factor(s). The second
42 argument is an optional `data=` argument that determines from which data frame
43 or data set the descriptive statistics are to be computed. This is illustrated
44 by the following example, which uses (like the page on item
45 objects, see `?item`) the GLES 2013 election study[^1]. In this example we
46 first create a table of some descriptives of the age distribution of the
47 respondents per German federal state:
48
49 ```r
50 library(memisc)
51 ZA5702 <- spss.system.file("Data/ZA5702_v2-0-0.sav",
52 ignore.scale.info=TRUE) # Because the measurement info in the file is wrong.
53 gles2013work <- subset(ZA5702,
54 select=c(
55 wave = survey,
56 gender = vn1,
57 byear = vn2c,
58 bmonth = vn2b,
59 intent.turnout = v10,
60 turnout = n10,
61 voteint.candidate = v11aa,
62 voteint.list = v11ba,
63 postal.vote.candidate = v12aa,
64 postal.vote.list = v12ba,
65 vote.candidate = n11aa,
66 vote.list = n11ba,
67 bula = bl
68 ))
69 gles2013work <- within(gles2013work,{
70 measurement(byear) <- "interval"
71 measurement(bmonth) <- "interval"
72 age <- 2013 - byear
73 age[bmonth > 9] <- age[bmonth > 9] - 1
74
75 })
76 options(digits=3)
77 age.tab <- genTable(c(Mean=mean(age),
78 `Std.dev`=sd(age),
79 Median=median(age))~bula,
80 data=gles2013work)
81 age.tab
82 ```
83
84 ```
85 bula
86 Baden-Wuerttemberg Bayern Berlin Brandenburg Bremen Hamburg Hessen
87 Mean 55 54 53 60 60 51 57
88 Std.dev 19 19 20 19 12 19 19
89 Median 57 56 57 62 63 53 60
90 bula
91 Mecklenburg-Vorpommern Niedersachsen Nordrhein-Westfalen
92 Mean 57 55 54
93 Std.dev 19 18 19
94 Median 60 56 55
95 bula
96 Rheinland-Pfalz Saarland Sachsen Sachsen-Anhalt Schleswig-Holstein
97 Mean 57 62 58 55 60
98 Std.dev 18 17 17 17 20
99 Median 60 65 60 56 65
100 bula
101 Thueringen
102 Mean 58
103 Std.dev 17
104 Median 60
105 ```
106 This table does not look good, so we transprose it:
107
108 ```r
109 age.tab <- t(age.tab)
110 age.tab
111 ```
112
113 ```
114 bula Mean Std.dev Median
115 Baden-Wuerttemberg 54.5 18.9 57.0
116 Bayern 54.4 18.9 56.0
117 Berlin 52.8 19.8 57.0
118 Brandenburg 59.7 19.3 62.5
119 Bremen 60.4 11.5 63.0
120 Hamburg 51.5 18.7 53.0
121 Hessen 56.9 18.5 60.0
122 Mecklenburg-Vorpommern 57.0 19.2 60.5
123 Niedersachsen 55.1 18.4 56.0
124 Nordrhein-Westfalen 53.9 19.1 55.0
125 Rheinland-Pfalz 57.2 18.2 60.5
126 Saarland 61.9 17.3 65.0
127 Sachsen 58.3 16.7 60.5
128 Sachsen-Anhalt 54.7 17.1 56.0
129 Schleswig-Holstein 60.0 19.9 65.0
130 Thueringen 57.8 17.4 60.0
131 ```
132 In the next example we create a table of percentages of the second votes per federal state. First we have to prepare the data, though:
133
134 ```r
135 gles2013work <- within(gles2013work,{
136
137 candidate.vote <- cases(
138 wave == 1 & intent.turnout == 6 -> postal.vote.candidate,
139 wave == 1 & intent.turnout %in% 4:5 -> 900,
140 wave == 1 & intent.turnout %in% 1:3 -> voteint.candidate,
141 wave == 2 & turnout == 1 -> vote.candidate,
142 wave == 2 & turnout == 2 -> 900
143 )
144
145 list.vote <- cases(
146 wave == 1 & intent.turnout == 6 -> postal.vote.list,
147 wave == 1 & intent.turnout %in% 4:5 -> 900,
148 wave == 1 & intent.turnout %in% 1:3 -> voteint.list,
149 wave == 2 & turnout ==1 -> vote.list,
150 wave == 2 & turnout ==2 -> 900
151 )
152
153 candidate.vote <- recode(as.item(candidate.vote),
154 "CDU/CSU" = 1 <- 1,
155 "SPD" = 2 <- 4,
156 "FDP" = 3 <- 5,
157 "Grüne" = 4 <- 6,
158 "Linke" = 5 <- 7,
159 "NPD" = 6 <- 206,
160 "Piraten" = 7 <- 215,
161 "AfD" = 8 <- 322,
162 "Other" = 10 <- 801,
163 "No Vote" = 90 <- 900,
164 "WN" = 98 <- -98,
165 "KA" = 99 <- -99
166 )
167 list.vote <- recode(as.item(list.vote),
168 "CDU/CSU" = 1 <- 1,
169 "SPD" = 2 <- 4,
170 "FDP" = 3 <- 5,
171 "Grüne" = 4 <- 6,
172 "Linke" = 5 <- 7,
173 "NPD" = 6 <- 206,
174 "Piraten" = 7 <- 215,
175 "AfD" = 8 <- 322,
176 "Other" = 10 <- 801,
177 "No Vote" = 90 <- 900,
178 "WN" = 98 <- -98,
179 "KA" = 99 <- -99
180 )
181
182 missing.values(candidate.vote) <- 98:99
183 missing.values(list.vote) <- 98:99
184 measurement(candidate.vote) <- "nominal"
185 measurement(list.vote) <- "nominal"
186 })
187 ```
188
189 ```
190 Warning messages:
191 1: In cases(postal.vote.candidate <- wave == 1 & intent.turnout == :
192 78 NAs created
193 2: In cases(postal.vote.list <- wave == 1 & intent.turnout == 6, 900 <- wave == :
194 78 NAs created
195 3: In recode(as.item(candidate.vote), `CDU/CSU` = 1 <- 1, SPD = 2 <- 4, :
196 recoding created 18 NAs
197 4: In recode(as.item(list.vote), `CDU/CSU` = 1 <- 1, SPD = 2 <- 4, :
198 recoding created 19 NAs
199 ```
200
201 (When the code is run, some warnings are issued, that indicate that the conditions are not exhaustive,
202 that is, there are some observations for which none of the conditions in the call `cases()`
203 are met. The corresponding elements of resulting vector will contain `NA` for these observations.
204 In the present case this occurs with observations that have missing values in both `intent.turnout` and `turnout`.)
205
206 After having set up the data, we get our table of percentages:
207
208 ```r
209 vote.tab <- genTable(percent(list.vote)~bula,
210 data=gles2013work)
211 options(digits=1)
212 t(vote.tab)
213 ```
214
215 ```
216 bula CDU/CSU SPD FDP Grüne Linke NPD Piraten AfD Other No Vote N
217 Baden-Wuerttemberg 28 22 7 17 6 0.4 2.1 4.6 1.1 12 285
218 Bayern 36 18 6 11 5 0.0 2.4 4.0 2.0 16 451
219 Berlin 27 22 8 10 14 1.8 1.8 6.6 0.6 8 166
220 Brandenburg 20 23 2 6 19 0.6 0.6 2.5 1.2 25 162
221 Bremen 22 26 0 17 13 0.0 0.0 4.3 0.0 17 23
222 Hamburg 22 36 2 4 7 2.2 0.0 4.4 2.2 20 45
223 Hessen 42 26 3 8 4 0.0 0.5 3.0 0.0 12 200
224 Mecklenburg-Vorpommern 33 20 2 4 18 1.4 2.7 1.4 0.0 18 146
225 Niedersachsen 33 32 3 10 3 0.0 0.7 0.7 0.4 17 284
226 Nordrhein-Westfalen 33 31 3 11 4 0.4 2.3 1.8 0.7 13 563
227 Rheinland-Pfalz 39 21 2 6 9 1.6 0.8 3.9 1.6 15 127
228 Saarland 40 40 0 0 0 0.0 0.0 0.0 0.0 20 30
229 Sachsen 49 17 1 3 14 0.3 1.2 0.9 0.3 13 332
230 Sachsen-Anhalt 27 29 1 8 19 0.4 0.8 0.4 0.0 13 241
231 Schleswig-Holstein 28 26 4 9 4 0.0 0.0 5.2 0.9 22 116
232 Thueringen 35 16 2 3 22 1.2 0.0 2.4 0.8 18 245
233 ```
234
235 It is of course also possible to create multi-dimensional tables, i.e. tables created by grouping by more than one factor:
236
237 ```r
238 gles2013work <- within(gles2013work,{
239
240 # We relabel the items, since they are originally in German
241 labels(turnout) <- c("Yes, voted"=1, "No, did not vote"=2)
242 labels(gender) <- c("Male"=1,"Female"=2)
243 })
244 genTable(percent(turnout)~gender+bula,
245 data=gles2013work)
246 ```
247
248 ```
249 , , bula = Baden-Wuerttemberg
250
251 gender
252 Male Female
253 Yes, voted 88 85
254 No, did not vote 12 15
255 N 90 61
256
257 , , bula = Bayern
258
259 gender
260 Male Female
261 Yes, voted 85 80
262 No, did not vote 15 20
263 N 89 129
264
265 , , bula = Berlin
266
267 gender
268 Male Female
269 Yes, voted 100 85
270 No, did not vote 0 15
271 N 38 52
272
273 , , bula = Brandenburg
274
275 gender
276 Male Female
277 Yes, voted 83 77
278 No, did not vote 17 23
279 N 36 62
280
281 , , bula = Bremen
282
283 gender
284 Male Female
285 Yes, voted 91 80
286 No, did not vote 9 20
287 N 11 5
288
289 , , bula = Hamburg
290
291 gender
292 Male Female
293 Yes, voted 88 76
294 No, did not vote 12 24
295 N 16 21
296
297 , , bula = Hessen
298
299 gender
300 Male Female
301 Yes, voted 91 81
302 No, did not vote 9 19
303 N 66 48
304
305 , , bula = Mecklenburg-Vorpommern
306
307 gender
308 Male Female
309 Yes, voted 84 72
310 No, did not vote 16 28
311 N 32 47
312
313 , , bula = Niedersachsen
314
315 gender
316 Male Female
317 Yes, voted 88 83
318 No, did not vote 12 17
319 N 75 70
320
321 , , bula = Nordrhein-Westfalen
322
323 gender
324 Male Female
325 Yes, voted 90 82
326 No, did not vote 10 18
327 N 148 158
328
329 , , bula = Rheinland-Pfalz
330
331 gender
332 Male Female
333 Yes, voted 84 85
334 No, did not vote 16 15
335 N 43 34
336
337 , , bula = Saarland
338
339 gender
340 Male Female
341 Yes, voted 91 72
342 No, did not vote 9 28
343 N 11 18
344
345 , , bula = Sachsen
346
347 gender
348 Male Female
349 Yes, voted 88 88
350 No, did not vote 12 12
351 N 103 73
352
353 , , bula = Sachsen-Anhalt
354
355 gender
356 Male Female
357 Yes, voted 89 81
358 No, did not vote 11 19
359 N 63 73
360
361 , , bula = Schleswig-Holstein
362
363 gender
364 Male Female
365 Yes, voted 89 85
366 No, did not vote 11 15
367 N 37 33
368
369 , , bula = Thueringen
370
371 gender
372 Male Female
373 Yes, voted 91 71
374 No, did not vote 9 29
375 N 70 73
376 ```
377
378 # Formatting Tables of Descriptive Statistics
379
380 The results of `genTable()` are objects of class `"table"` so that they can be
381 re-arranged into a "flattened" table by the function `ftable`. To demonstrate
382 this, we continue the previous example:
383
384 ```r
385 gt <- genTable(percent(turnout)~gender+bula,
386 data=gles2013work)
387 # We beautify the table a bit ...
388 names(dimnames(gt)) <- c("Voted","Gender","State")
389 gt <- dimrename(gt,"Yes, voted"="Yes",
390 "No, did not vote"="No")
391 ftable(gt,col.vars = c("Gender","Voted"))
392 ```
393
394 ```
395 Gender Male Female
396 Voted Yes No N Yes No N
397 State
398 Baden-Wuerttemberg 88 12 90 85 15 61
399 Bayern 85 15 89 80 20 129
400 Berlin 100 0 38 85 15 52
401 Brandenburg 83 17 36 77 23 62
402 Bremen 91 9 11 80 20 5
403 Hamburg 88 12 16 76 24 21
404 Hessen 91 9 66 81 19 48
405 Mecklenburg-Vorpommern 84 16 32 72 28 47
406 Niedersachsen 88 12 75 83 17 70
407 Nordrhein-Westfalen 90 10 148 82 18 158
408 Rheinland-Pfalz 84 16 43 85 15 34
409 Saarland 91 9 11 72 28 18
410 Sachsen 88 12 103 88 12 73
411 Sachsen-Anhalt 89 11 63 81 19 73
412 Schleswig-Holstein 89 11 37 85 15 33
413 Thueringen 91 9 70 71 29 73
414 ```
415
416 Arranging the cells of a table using `ftable()` improves the appearance of the
417 results of `genTable()` on screen, but to include the results into a word
418 processor document or a LaTeX file, further facilities are needed and provided
419 by "memisc". To include the flattened table into a LaTeX document, one can
420 convert and store it in the appropriate format using `toLatex()` and
421 `writeLines()`
422
423 ```r
424 ft <- ftable(gt,col.vars = c("Gender","Voted"))
425 lt <- toLatex(ft,digits=c(1,1,0,1,1,0))
426 writeLines(lt,con="Voted2013-GenderState.tex")
427 ```
428
429
430 For HTML output, one can use `show_html()` (e.g. for inclusion in "knitr"
431 documents) and `write_html()`, both functions being based on
432 `format_html()`. Here we continue the example to demonstate this:
433
434 ```r
435 show_html(ft,digits=c(1,1,0,1,1,0))
436 ```
437
438 ```{=html}
439 <style>
440 table#ftable-3 {
441 border-collapse: collapse; border-style: none; margin: 2ex auto;
442 }
443 table#ftable-3 tr {
444 border-style: none;
445 }
446 table#ftable-3 td {
447 padding-top: 3px;
448 padding-bottom: 3px;
449 padding-left: 0.5ex;
450 padding-right: 0.5ex;
451 margin: 0px;
452 margin-top: 0px;
453 margin-bottom: 0px;
454 border-style: none;
455 border-width: 0;
456 }
457 table#ftable-3 tr:nth-child(1){
458 border-top: 2px solid;
459 }
460 table#ftable-3 tr:nth-child(2){
461 border-bottom: 1px solid;
462 }
463 table#ftable-3 tr:nth-child(18){
464 border-bottom: 2px solid;
465 }
466 table#ftable-3 tr:nth-child(1) td:nth-child(n+3){
467 border-bottom: 1px solid;
468 }
469 table#ftable-3 td:nth-child(4),
470 table#ftable-3 td:nth-child(7),
471 table#ftable-3 td:nth-child(10),
472 table#ftable-3 td:nth-child(13),
473 table#ftable-3 td:nth-child(16),
474 table#ftable-3 td:nth-child(19){
475 padding-left: 0px;
476 padding-right: 0px;
477 text-align: center;
478 width: .2ex;
479 /*background-color: blue;*/
480 }
481 table#ftable-3 td:nth-child(3),
482 table#ftable-3 td:nth-child(6),
483 table#ftable-3 td:nth-child(9),
484 table#ftable-3 td:nth-child(12),
485 table#ftable-3 td:nth-child(15),
486 table#ftable-3 td:nth-child(18){
487 padding-left: 0.5ex;
488 padding-right: 0px;
489 text-align: right;
490 /*background-color: red;*/
491 }
492 table#ftable-3 td:nth-child(5),
493 table#ftable-3 td:nth-child(8),
494 table#ftable-3 td:nth-child(11),
495 table#ftable-3 td:nth-child(14),
496 table#ftable-3 td:nth-child(17),
497 table#ftable-3 td:nth-child(20){
498 padding-left: 0px;
499 padding-right: 0.5ex;
500 text-align: left;
501 /*background-color: green;*/
502 }
503 table#ftable-3 td:nth-child(n of .header){
504 padding-left: 0.5ex;
505 padding-right: 0.5ex;
506 text-align: center;
507 /*background-color: gray;*/
508 }
509 </style>
510 <table class="ftable" id="ftable-3">
511 <tr><td></td><td>Gender:</td><td colspan="9" class="header">Male</td><td colspan="9" class="header">Female</td></tr>
512 <tr><td>State</td><td>Voted:</td><td colspan="3" class="header">Yes</td><td colspan="3" class="header">No</td><td colspan="3" class="header">N</td><td colspan="3" class="header">Yes</td><td colspan="3" class="header">No</td><td colspan="3" class="header">N</td></tr>
513 <tr><td>Baden-Wuerttemberg</td><td></td><td>87</td><td>.</td><td>8</td><td>12</td><td>.</td><td>2</td><td>90</td><td></td><td></td><td>85</td><td>.</td><td>2</td><td>14</td><td>.</td><td>8</td><td>61</td><td></td><td></td></tr>
514 <tr><td>Bayern</td><td></td><td>85</td><td>.</td><td>4</td><td>14</td><td>.</td><td>6</td><td>89</td><td></td><td></td><td>79</td><td>.</td><td>8</td><td>20</td><td>.</td><td>2</td><td>129</td><td></td><td></td></tr>
515 <tr><td>Berlin</td><td></td><td>100</td><td>.</td><td>0</td><td>0</td><td>.</td><td>0</td><td>38</td><td></td><td></td><td>84</td><td>.</td><td>6</td><td>15</td><td>.</td><td>4</td><td>52</td><td></td><td></td></tr>
516 <tr><td>Brandenburg</td><td></td><td>83</td><td>.</td><td>3</td><td>16</td><td>.</td><td>7</td><td>36</td><td></td><td></td><td>77</td><td>.</td><td>4</td><td>22</td><td>.</td><td>6</td><td>62</td><td></td><td></td></tr>
517 <tr><td>Bremen</td><td></td><td>90</td><td>.</td><td>9</td><td>9</td><td>.</td><td>1</td><td>11</td><td></td><td></td><td>80</td><td>.</td><td>0</td><td>20</td><td>.</td><td>0</td><td>5</td><td></td><td></td></tr>
518 <tr><td>Hamburg</td><td></td><td>87</td><td>.</td><td>5</td><td>12</td><td>.</td><td>5</td><td>16</td><td></td><td></td><td>76</td><td>.</td><td>2</td><td>23</td><td>.</td><td>8</td><td>21</td><td></td><td></td></tr>
519 <tr><td>Hessen</td><td></td><td>90</td><td>.</td><td>9</td><td>9</td><td>.</td><td>1</td><td>66</td><td></td><td></td><td>81</td><td>.</td><td>2</td><td>18</td><td>.</td><td>8</td><td>48</td><td></td><td></td></tr>
520 <tr><td>Mecklenburg-Vorpommern</td><td></td><td>84</td><td>.</td><td>4</td><td>15</td><td>.</td><td>6</td><td>32</td><td></td><td></td><td>72</td><td>.</td><td>3</td><td>27</td><td>.</td><td>7</td><td>47</td><td></td><td></td></tr>
521 <tr><td>Niedersachsen</td><td></td><td>88</td><td>.</td><td>0</td><td>12</td><td>.</td><td>0</td><td>75</td><td></td><td></td><td>82</td><td>.</td><td>9</td><td>17</td><td>.</td><td>1</td><td>70</td><td></td><td></td></tr>
522 <tr><td>Nordrhein-Westfalen</td><td></td><td>89</td><td>.</td><td>9</td><td>10</td><td>.</td><td>1</td><td>148</td><td></td><td></td><td>82</td><td>.</td><td>3</td><td>17</td><td>.</td><td>7</td><td>158</td><td></td><td></td></tr>
523 <tr><td>Rheinland-Pfalz</td><td></td><td>83</td><td>.</td><td>7</td><td>16</td><td>.</td><td>3</td><td>43</td><td></td><td></td><td>85</td><td>.</td><td>3</td><td>14</td><td>.</td><td>7</td><td>34</td><td></td><td></td></tr>
524 <tr><td>Saarland</td><td></td><td>90</td><td>.</td><td>9</td><td>9</td><td>.</td><td>1</td><td>11</td><td></td><td></td><td>72</td><td>.</td><td>2</td><td>27</td><td>.</td><td>8</td><td>18</td><td></td><td></td></tr>
525 <tr><td>Sachsen</td><td></td><td>88</td><td>.</td><td>3</td><td>11</td><td>.</td><td>7</td><td>103</td><td></td><td></td><td>87</td><td>.</td><td>7</td><td>12</td><td>.</td><td>3</td><td>73</td><td></td><td></td></tr>
526 <tr><td>Sachsen-Anhalt</td><td></td><td>88</td><td>.</td><td>9</td><td>11</td><td>.</td><td>1</td><td>63</td><td></td><td></td><td>80</td><td>.</td><td>8</td><td>19</td><td>.</td><td>2</td><td>73</td><td></td><td></td></tr>
527 <tr><td>Schleswig-Holstein</td><td></td><td>89</td><td>.</td><td>2</td><td>10</td><td>.</td><td>8</td><td>37</td><td></td><td></td><td>84</td><td>.</td><td>8</td><td>15</td><td>.</td><td>2</td><td>33</td><td></td><td></td></tr>
528 <tr><td>Thueringen</td><td></td><td>91</td><td>.</td><td>4</td><td>8</td><td>.</td><td>6</td><td>70</td><td></td><td></td><td>71</td><td>.</td><td>2</td><td>28</td><td>.</td><td>8</td><td>73</td><td></td><td></td></tr>
529 </table>
530 ```
531
532 ```r
533 show_html(ft,digits=c(1,1,0,1,1,0),show.titles=FALSE)
534 ```
535
536 ```{=html}
537 <style>
538 table#ftable-4 {
539 border-collapse: collapse; border-style: none; margin: 2ex auto;
540 }
541 table#ftable-4 tr {
542 border-style: none;
543 }
544 table#ftable-4 td {
545 padding-top: 3px;
546 padding-bottom: 3px;
547 padding-left: 0.5ex;
548 padding-right: 0.5ex;
549 margin: 0px;
550 margin-top: 0px;
551 margin-bottom: 0px;
552 border-style: none;
553 border-width: 0;
554 }
555 table#ftable-4 tr:nth-child(1){
556 border-top: 2px solid;
557 }
558 table#ftable-4 tr:nth-child(2){
559 border-bottom: 1px solid;
560 }
561 table#ftable-4 tr:nth-child(18){
562 border-bottom: 2px solid;
563 }
564 table#ftable-4 tr:nth-child(1) td:nth-child(n+2){
565 border-bottom: 1px solid;
566 }
567 table#ftable-4 td:nth-child(3),
568 table#ftable-4 td:nth-child(6),
569 table#ftable-4 td:nth-child(9),
570 table#ftable-4 td:nth-child(12),
571 table#ftable-4 td:nth-child(15),
572 table#ftable-4 td:nth-child(18){
573 padding-left: 0px;
574 padding-right: 0px;
575 text-align: center;
576 width: .2ex;
577 /*background-color: blue;*/
578 }
579 table#ftable-4 td:nth-child(2),
580 table#ftable-4 td:nth-child(5),
581 table#ftable-4 td:nth-child(8),
582 table#ftable-4 td:nth-child(11),
583 table#ftable-4 td:nth-child(14),
584 table#ftable-4 td:nth-child(17){
585 padding-left: 0.5ex;
586 padding-right: 0px;
587 text-align: right;
588 /*background-color: red;*/
589 }
590 table#ftable-4 td:nth-child(4),
591 table#ftable-4 td:nth-child(7),
592 table#ftable-4 td:nth-child(10),
593 table#ftable-4 td:nth-child(13),
594 table#ftable-4 td:nth-child(16),
595 table#ftable-4 td:nth-child(19){
596 padding-left: 0px;
597 padding-right: 0.5ex;
598 text-align: left;
599 /*background-color: green;*/
600 }
601 table#ftable-4 td:nth-child(n of .header){
602 padding-left: 0.5ex;
603 padding-right: 0.5ex;
604 text-align: center;
605 /*background-color: gray;*/
606 }
607 </style>
608 <table class="ftable" id="ftable-4">
609 <tr><td></td><td colspan="9" class="header">Male</td><td colspan="9" class="header">Female</td></tr>
610 <tr><td></td><td colspan="3" class="header">Yes</td><td colspan="3" class="header">No</td><td colspan="3" class="header">N</td><td colspan="3" class="header">Yes</td><td colspan="3" class="header">No</td><td colspan="3" class="header">N</td></tr>
611 <tr><td>Baden-Wuerttemberg</td><td>87</td><td>.</td><td>8</td><td>12</td><td>.</td><td>2</td><td>90</td><td></td><td></td><td>85</td><td>.</td><td>2</td><td>14</td><td>.</td><td>8</td><td>61</td><td></td><td></td></tr>
612 <tr><td>Bayern</td><td>85</td><td>.</td><td>4</td><td>14</td><td>.</td><td>6</td><td>89</td><td></td><td></td><td>79</td><td>.</td><td>8</td><td>20</td><td>.</td><td>2</td><td>129</td><td></td><td></td></tr>
613 <tr><td>Berlin</td><td>100</td><td>.</td><td>0</td><td>0</td><td>.</td><td>0</td><td>38</td><td></td><td></td><td>84</td><td>.</td><td>6</td><td>15</td><td>.</td><td>4</td><td>52</td><td></td><td></td></tr>
614 <tr><td>Brandenburg</td><td>83</td><td>.</td><td>3</td><td>16</td><td>.</td><td>7</td><td>36</td><td></td><td></td><td>77</td><td>.</td><td>4</td><td>22</td><td>.</td><td>6</td><td>62</td><td></td><td></td></tr>
615 <tr><td>Bremen</td><td>90</td><td>.</td><td>9</td><td>9</td><td>.</td><td>1</td><td>11</td><td></td><td></td><td>80</td><td>.</td><td>0</td><td>20</td><td>.</td><td>0</td><td>5</td><td></td><td></td></tr>
616 <tr><td>Hamburg</td><td>87</td><td>.</td><td>5</td><td>12</td><td>.</td><td>5</td><td>16</td><td></td><td></td><td>76</td><td>.</td><td>2</td><td>23</td><td>.</td><td>8</td><td>21</td><td></td><td></td></tr>
617 <tr><td>Hessen</td><td>90</td><td>.</td><td>9</td><td>9</td><td>.</td><td>1</td><td>66</td><td></td><td></td><td>81</td><td>.</td><td>2</td><td>18</td><td>.</td><td>8</td><td>48</td><td></td><td></td></tr>
618 <tr><td>Mecklenburg-Vorpommern</td><td>84</td><td>.</td><td>4</td><td>15</td><td>.</td><td>6</td><td>32</td><td></td><td></td><td>72</td><td>.</td><td>3</td><td>27</td><td>.</td><td>7</td><td>47</td><td></td><td></td></tr>
619 <tr><td>Niedersachsen</td><td>88</td><td>.</td><td>0</td><td>12</td><td>.</td><td>0</td><td>75</td><td></td><td></td><td>82</td><td>.</td><td>9</td><td>17</td><td>.</td><td>1</td><td>70</td><td></td><td></td></tr>
620 <tr><td>Nordrhein-Westfalen</td><td>89</td><td>.</td><td>9</td><td>10</td><td>.</td><td>1</td><td>148</td><td></td><td></td><td>82</td><td>.</td><td>3</td><td>17</td><td>.</td><td>7</td><td>158</td><td></td><td></td></tr>
621 <tr><td>Rheinland-Pfalz</td><td>83</td><td>.</td><td>7</td><td>16</td><td>.</td><td>3</td><td>43</td><td></td><td></td><td>85</td><td>.</td><td>3</td><td>14</td><td>.</td><td>7</td><td>34</td><td></td><td></td></tr>
622 <tr><td>Saarland</td><td>90</td><td>.</td><td>9</td><td>9</td><td>.</td><td>1</td><td>11</td><td></td><td></td><td>72</td><td>.</td><td>2</td><td>27</td><td>.</td><td>8</td><td>18</td><td></td><td></td></tr>
623 <tr><td>Sachsen</td><td>88</td><td>.</td><td>3</td><td>11</td><td>.</td><td>7</td><td>103</td><td></td><td></td><td>87</td><td>.</td><td>7</td><td>12</td><td>.</td><td>3</td><td>73</td><td></td><td></td></tr>
624 <tr><td>Sachsen-Anhalt</td><td>88</td><td>.</td><td>9</td><td>11</td><td>.</td><td>1</td><td>63</td><td></td><td></td><td>80</td><td>.</td><td>8</td><td>19</td><td>.</td><td>2</td><td>73</td><td></td><td></td></tr>
625 <tr><td>Schleswig-Holstein</td><td>89</td><td>.</td><td>2</td><td>10</td><td>.</td><td>8</td><td>37</td><td></td><td></td><td>84</td><td>.</td><td>8</td><td>15</td><td>.</td><td>2</td><td>33</td><td></td><td></td></tr>
626 <tr><td>Thueringen</td><td>91</td><td>.</td><td>4</td><td>8</td><td>.</td><td>6</td><td>70</td><td></td><td></td><td>71</td><td>.</td><td>2</td><td>28</td><td>.</td><td>8</td><td>73</td><td></td><td></td></tr>
627 </table>
628 ```
629
630 ```r
631 # Writing into a HTML file ...
632 write_html(ft,digits=c(1,1,0,1,1,0),show.titles=FALSE,
633 file="Voted2013-GenderState.html")
634 ```
635 Continuing another example:
636
637 ```r
638 # age.tab was created earlier
639 age.ftab <- ftable(age.tab,row.vars=2)
640 show_html(age.ftab,digits=1,show.titles=FALSE)
641 ```
642
643 ```{=html}
644 <style>
645 table#ftable-6 {
646 border-collapse: collapse; border-style: none; margin: 2ex auto;
647 }
648 table#ftable-6 tr {
649 border-style: none;
650 }
651 table#ftable-6 td {
652 padding-top: 3px;
653 padding-bottom: 3px;
654 padding-left: 0.5ex;
655 padding-right: 0.5ex;
656 margin: 0px;
657 margin-top: 0px;
658 margin-bottom: 0px;
659 border-style: none;
660 border-width: 0;
661 }
662 table#ftable-6 tr:nth-child(1){
663 border-top: 2px solid;
664 }
665 table#ftable-6 tr:nth-child(1){
666 border-bottom: 1px solid;
667 }
668 table#ftable-6 tr:nth-child(17){
669 border-bottom: 2px solid;
670 }
671 table#ftable-6 td:nth-child(3),
672 table#ftable-6 td:nth-child(6),
673 table#ftable-6 td:nth-child(9){
674 padding-left: 0px;
675 padding-right: 0px;
676 text-align: center;
677 width: .2ex;
678 /*background-color: blue;*/
679 }
680 table#ftable-6 td:nth-child(2),
681 table#ftable-6 td:nth-child(5),
682 table#ftable-6 td:nth-child(8){
683 padding-left: 0.5ex;
684 padding-right: 0px;
685 text-align: right;
686 /*background-color: red;*/
687 }
688 table#ftable-6 td:nth-child(4),
689 table#ftable-6 td:nth-child(7),
690 table#ftable-6 td:nth-child(10){
691 padding-left: 0px;
692 padding-right: 0.5ex;
693 text-align: left;
694 /*background-color: green;*/
695 }
696 table#ftable-6 td:nth-child(n of .header){
697 padding-left: 0.5ex;
698 padding-right: 0.5ex;
699 text-align: center;
700 /*background-color: gray;*/
701 }
702 </style>
703 <table class="ftable" id="ftable-6">
704 <tr><td></td><td colspan="3" class="header">Mean</td><td colspan="3" class="header">Std.dev</td><td colspan="3" class="header">Median</td></tr>
705 <tr><td>Baden-Wuerttemberg</td><td>54</td><td>.</td><td>5</td><td>18</td><td>.</td><td>9</td><td>57</td><td>.</td><td>0</td></tr>
706 <tr><td>Bayern</td><td>54</td><td>.</td><td>4</td><td>18</td><td>.</td><td>9</td><td>56</td><td>.</td><td>0</td></tr>
707 <tr><td>Berlin</td><td>52</td><td>.</td><td>8</td><td>19</td><td>.</td><td>8</td><td>57</td><td>.</td><td>0</td></tr>
708 <tr><td>Brandenburg</td><td>59</td><td>.</td><td>7</td><td>19</td><td>.</td><td>3</td><td>62</td><td>.</td><td>5</td></tr>
709 <tr><td>Bremen</td><td>60</td><td>.</td><td>4</td><td>11</td><td>.</td><td>5</td><td>63</td><td>.</td><td>0</td></tr>
710 <tr><td>Hamburg</td><td>51</td><td>.</td><td>5</td><td>18</td><td>.</td><td>7</td><td>53</td><td>.</td><td>0</td></tr>
711 <tr><td>Hessen</td><td>56</td><td>.</td><td>9</td><td>18</td><td>.</td><td>5</td><td>60</td><td>.</td><td>0</td></tr>
712 <tr><td>Mecklenburg-Vorpommern</td><td>57</td><td>.</td><td>0</td><td>19</td><td>.</td><td>2</td><td>60</td><td>.</td><td>5</td></tr>
713 <tr><td>Niedersachsen</td><td>55</td><td>.</td><td>1</td><td>18</td><td>.</td><td>4</td><td>56</td><td>.</td><td>0</td></tr>
714 <tr><td>Nordrhein-Westfalen</td><td>53</td><td>.</td><td>9</td><td>19</td><td>.</td><td>1</td><td>55</td><td>.</td><td>0</td></tr>
715 <tr><td>Rheinland-Pfalz</td><td>57</td><td>.</td><td>2</td><td>18</td><td>.</td><td>2</td><td>60</td><td>.</td><td>5</td></tr>
716 <tr><td>Saarland</td><td>61</td><td>.</td><td>9</td><td>17</td><td>.</td><td>3</td><td>65</td><td>.</td><td>0</td></tr>
717 <tr><td>Sachsen</td><td>58</td><td>.</td><td>3</td><td>16</td><td>.</td><td>7</td><td>60</td><td>.</td><td>5</td></tr>
718 <tr><td>Sachsen-Anhalt</td><td>54</td><td>.</td><td>7</td><td>17</td><td>.</td><td>1</td><td>56</td><td>.</td><td>0</td></tr>
719 <tr><td>Schleswig-Holstein</td><td>60</td><td>.</td><td>0</td><td>19</td><td>.</td><td>9</td><td>65</td><td>.</td><td>0</td></tr>
720 <tr><td>Thueringen</td><td>57</td><td>.</td><td>8</td><td>17</td><td>.</td><td>4</td><td>60</td><td>.</td><td>0</td></tr>
721 </table>
722 ```
723
724 Of course we can also export to LaTeX:
725
726 ```r
727 toLatex(age.ftab,digits=1,show.titles=FALSE)
728 ```
729
730 ```
731 \begin{tabular}{llD{.}{.}{1}D{.}{.}{1}D{.}{.}{1}}
732 \toprule
733 && \multicolumn{1}{c}{Mean}&\multicolumn{1}{c}{Std.dev}&\multicolumn{1}{c}{Median}\\
734 \midrule
735 Baden-Wuerttemberg && 54.5 & 18.9 & 57.0\\
736 Bayern && 54.4 & 18.9 & 56.0\\
737 Berlin && 52.8 & 19.8 & 57.0\\
738 Brandenburg && 59.7 & 19.3 & 62.5\\
739 Bremen && 60.4 & 11.5 & 63.0\\
740 Hamburg && 51.5 & 18.7 & 53.0\\
741 Hessen && 56.9 & 18.5 & 60.0\\
742 Mecklenburg-Vorpommern && 57.0 & 19.2 & 60.5\\
743 Niedersachsen && 55.1 & 18.4 & 56.0\\
744 Nordrhein-Westfalen && 53.9 & 19.1 & 55.0\\
745 Rheinland-Pfalz && 57.2 & 18.2 & 60.5\\
746 Saarland && 61.9 & 17.3 & 65.0\\
747 Sachsen && 58.3 & 16.7 & 60.5\\
748 Sachsen-Anhalt && 54.7 & 17.1 & 56.0\\
749 Schleswig-Holstein && 60.0 & 19.9 & 65.0\\
750 Thueringen && 57.8 & 17.4 & 60.0\\
751 \bottomrule
752 \end{tabular}
753 ```
754
755
756
757 [^1]: The [German Longitudinal Election
758 Study](https://www.gesis.org/gles/about-gles) is funded by the German National
759 Science Foundation (DFG) and carried out outin close cooperation with the
760 [DGfW](https://www.dgfw.info/), German Society for Electoral Studies. Principal
761 investigators are Hans Rattinger (University of Mannheim, until 2014), Sigrid
762 Roßteutscher (University of Frankfurt), Rüdiger Schmitt-Beck (University of
763 Mannheim), Harald Schoen (Mannheim Centre for European Social Research, from
764 2015), Bernhard Weßels (Social Science Research Center Berlin), and Christof
765 Wolf (GESIS – Leibniz Institute for the Social Sciences, since 2012). Neither
766 the funding organisation nor the principal investigators bear any responsibility
767 for the example code shown here.