Creating nicely styled cross tables in R can be tedios, even when you use the styledTables
package. Therefore, we implemented the following functions, which make it very easy to create cross tables from [tidy data sets]:
create_cross_table_header()
: This function takes a data.frame
and creates the styled header rows of a cross table. The cross table can have multiple hirachy levels.create_cross_table_body()
: This function takes a data.frame
and creates the styled body rows of a cross table. The cross table can have multiple hirachy levels.styled_table()
: This function can be used to vertically concatenate the generated styled cross table header with the generated styled cross table body.We will need a [tidy data set] to illustrate the power of styledTables
cross table creation. Therefore, we create a data.frame
named students
, which holds some fictional test results of students coming from Germany and Austria in subjects Mathematics and Statistics over several years. In the column N
the number of cases and in the column rel
the portion in percent is given.
library(dplyr)
students <- data.frame(
country = rep(c("Germany", "Austria"), each = 16),
year = c(rep("2010", 8), rep("2011", 8)),
subject = rep(c(rep("Mathematics", 4), rep("Statistics", 4)), 2),
gender = rep(c(rep("Male", 2), rep("Female", 2)), 4),
result = rep(c("positive", "negative"), 16),
N = sample(1:1000, 32)
) %>%
group_by(country, year, subject, gender) %>%
mutate(rel = 100 * N / sum(N)) %>%
ungroup
country | year | subject | gender | result | N | rel |
---|---|---|---|---|---|---|
Germany | 2010 | Mathematics | Male | positive | 121 | 29.297821 |
Germany | 2010 | Mathematics | Male | negative | 292 | 70.702179 |
Germany | 2010 | Mathematics | Female | positive | 355 | 40.158371 |
Germany | 2010 | Mathematics | Female | negative | 529 | 59.841629 |
Germany | 2010 | Statistics | Male | positive | 890 | 61.464088 |
Germany | 2010 | Statistics | Male | negative | 558 | 38.535912 |
Germany | 2010 | Statistics | Female | positive | 712 | 75.825346 |
Germany | 2010 | Statistics | Female | negative | 227 | 24.174654 |
Germany | 2011 | Mathematics | Male | positive | 591 | 56.555024 |
Germany | 2011 | Mathematics | Male | negative | 454 | 43.444976 |
Germany | 2011 | Mathematics | Female | positive | 411 | 65.238095 |
Germany | 2011 | Mathematics | Female | negative | 219 | 34.761905 |
Germany | 2011 | Statistics | Male | positive | 982 | 50.907206 |
Germany | 2011 | Statistics | Male | negative | 947 | 49.092794 |
Germany | 2011 | Statistics | Female | positive | 894 | 53.151011 |
Germany | 2011 | Statistics | Female | negative | 788 | 46.848989 |
Austria | 2010 | Mathematics | Male | positive | 975 | 91.981132 |
Austria | 2010 | Mathematics | Male | negative | 85 | 8.018868 |
Austria | 2010 | Mathematics | Female | positive | 767 | 58.194234 |
Austria | 2010 | Mathematics | Female | negative | 551 | 41.805766 |
Austria | 2010 | Statistics | Male | positive | 86 | 73.504273 |
Austria | 2010 | Statistics | Male | negative | 31 | 26.495726 |
Austria | 2010 | Statistics | Female | positive | 755 | 71.092279 |
Austria | 2010 | Statistics | Female | negative | 307 | 28.907721 |
Austria | 2011 | Mathematics | Male | positive | 827 | 59.325682 |
Austria | 2011 | Mathematics | Male | negative | 567 | 40.674318 |
Austria | 2011 | Mathematics | Female | positive | 901 | 54.375377 |
Austria | 2011 | Mathematics | Female | negative | 756 | 45.624623 |
Austria | 2011 | Statistics | Male | positive | 656 | 77.817319 |
Austria | 2011 | Statistics | Male | negative | 187 | 22.182681 |
Austria | 2011 | Statistics | Female | positive | 649 | 56.929825 |
Austria | 2011 | Statistics | Female | negative | 491 | 43.070175 |