# Example data frame
my_data <- dummy_data(1000)
# Define titles and footnotes. If you want to add hyperlinks you can do so by
# adding "link:" followed by the hyperlink to the main text.
titles <- c("This is title number 1 link: https://cran.r-project.org/",
"This is title number 2",
"This is title number 3")
footnotes <- c("This is footnote number 1",
"This is footnote number 2",
"This is footnote number 3 link: https://cran.r-project.org/")
# Output cross tables
my_data |> crosstabs(age, sex)
my_data |> crosstabs(age, sex,
weight = "weight")
# Also works with characters
my_data |> crosstabs("age", "sex")
my_data |> crosstabs("age", "sex",
weight = "weight")
# Applying formats and titles
age. <- discrete_format(
"Total" = 0:100,
"under 18" = 0:17,
"18 to under 25" = 18:24,
"25 to under 55" = 25:54,
"55 to under 65" = 55:64,
"65 and older" = 65:100)
sex. <- discrete_format(
"Total" = 1:2,
"Male" = 1,
"Female" = 2)
my_data |> crosstabs(age, sex,
formats = list(age = age., sex = sex.),
titles = titles,
footnotes = footnotes)
# Split cross table by expressions of another variable
my_data |> crosstabs(age, sex, by = education)
# Compute different stats
my_data |> crosstabs(age, sex,
statistics = c("sum", "freq", "pct_row", "pct_column", "pct_total"))
# Get a list with two data tables for further usage
result_list <- my_data |> crosstabs(age, sex,
formats = list(age = age., sex = sex.))
# Output in text file
my_data |> crosstabs(age, sex, output = "text")
# Output to Excel
my_data |> crosstabs(age, sex, output = "excel")
# With individual styling
my_style <- excel_output_style(header_back_color = "0077B6",
font = "Times New Roman")
my_data |> crosstabs(age, sex, output = "excel", style = my_style)
Run the code above in your browser using DataLab