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