create_cross_table_body | R Documentation |
This function creates the styled cross table body from a given data.frame.
This function works best in combination with the method create_cross_table_header()
and styled_table()
.
For the the creation of the styled cross table body the columns specified in sub_table_cols
are used to separate the
table body into several sub tables, where each sub table has the current
value in the column defined by sub_table_cols
as sub heading.
The columns specified in y_cols_left
, x_cols
and value_cols
will be used for the cross table calculation.
If the table should not be a cross table, but a normal table then the
arguments x_cols
and value_cols
should be omitted.
create_cross_table_body(data, ...)
## S4 method for signature 'data.frame'
create_cross_table_body(
data,
sub_table_cols = NULL,
sub_heading_stylings = NULL,
body_styling = NULL,
y_cols_left = NULL,
y_cols_right = NULL,
x_cols = NULL,
value_cols = NULL,
value_col_stylings = NULL,
drop_missing_rows = TRUE,
drop_missing_cols = FALSE,
aggregation = NULL,
fill_values = NULL
)
data |
A data.frame that should be used for the creation of the cross table. |
... |
Various arguments |
sub_table_cols |
(optional) A vector of column names that should be used in order to separate the data.frame into several sub tables, that will be concatenated vertically. Each sub table will have a sub heading row above the sub table body. The sub heading text is just the corresponding value in the column defined by |
sub_heading_stylings |
(optional) A list of styling functions. A styling function is a function that takes a StyledTable object as its only argument and returns a styled function. The first styling function is applied to the level-1 sub heading (the sub heading defined by the first entry in |
body_styling |
(optional) A styling function that is applied to the generated styled body of each sub table. A styling function is a function that takes a StyledTable object as its only argument and returns a styled function. In order to format the value columns of the resulting cross table use the argument |
y_cols_left |
(optional) A vector of column names that are unchanged by the cross table compuation and are printed to the left of the resulting cross table columns. This argument can be omitted, if no Y columns to the left of the cross_table are needed, but it is not allowed to omit both |
y_cols_right |
(optional) A vector of column names that are unchanged by the cross table compuation and that are printed to the right of the cross table. This argument can be omitted, if no Y columns to the right of the cross_table are needed, but it is not allowed to omit both |
x_cols |
(optional) A vector of column names that are used for column name generation by the cross table compuation. If the table should not be a cross table, but a normal table this argument can be omitted. |
value_cols |
(optional) A vector of column names that are used as value columns by the cross table compuation. Normally this is just one column. If it is a vector with several column names, then the column names of these columns introduce an extra level in the cross table column hirachy. |
value_col_stylings |
(optional) A single column styling function or a list of column styling functions that has the same length as |
drop_missing_rows |
(optional) If set to |
drop_missing_cols |
(optional) If set to |
aggregation |
(optional) Defines the function that will be used for the cross table cell aggregation. If not defined, the default aggregation function of the |
fill_values |
(optional) A list of values that is used to fill the missing values in the value columns in the calculated cross table. The first list value is used for the first value column, the second value for the second value column. Hence, the list given in |
The generated StyledTable object holding the styled table body rows
## Not run:
library(dplyr)
# prepare data set for cross table
students_data <- 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
# setup styled header
s_header <- create_cross_table_header(
y_col_headings = c("Year", "Subject"),
cross_table_heading = "Comparison of test results",
c("Male", "Female"),
c("Positive", "Negative"),
c("count", "in %")
) %>%
format_stat_header
# setup styled cross table body
s_body <- create_cross_table_body(
data = students_data,
sub_table_cols = "country",
sub_heading_styling = function(st) set_horizontal(st, "center"),
body_styling = function(st) set_horizontal(st, "center"),
y_cols_left = c("year", "subject"),
x_cols = c("gender", "result"),
value_cols = c("N", "rel"),
value_col_stylings = list(
format_stat_absolute,
format_stat_relative
),
fill_values = list(0L, 0)
)
# Concat styled header and styled body into a single styled table
s_tbl <- styled_table(
s_header,
s_body
)
s_tbl %>%
write_png
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.