This function combines two tables: one with raw counts and another with percentages (or other calculated values). It returns a formatted table that merges these values in a "count (percentage)" format.

combine_table(tab1, tab2, percent = TRUE)

Arguments

tab1

A data frame or matrix of the first statistics.

tab2

A data frame or matrix of the second statistics to be combined with tab1.

percent

Logical. If TRUE, formats tab2 as percentages (i.e., appends a % sign). If FALSE, combines the counts and values without the percentage sign.

Value

A data frame with the combined counts and percentages (or values).

Details

This function takes two tables, converts them to data frames, and then combines their values in a "count (percentage)" format. If the percent argument is TRUE, the values in tab2 are treated as percentages; otherwise, they are combined as raw values.

Examples

tab1<-margin.table(Titanic,c(1,4))
tab2 <- round(prop.table(tab1, margin = 1) * 100, 2)
combine_table(tab1,tab2,)
#>                No          Yes
#> 1st  122 (37.54%) 203 (62.46%)
#> 2nd   167 (58.6%)  118 (41.4%)
#> 3rd  528 (74.79%) 178 (25.21%)
#> Crew 673 (76.05%) 212 (23.95%)