mergeDesc: Prepares a matrix for htmlTable from a list

Description Usage Arguments Value The rgroup value The colnames of the matrix Examples

View source: R/mergeDesc.R

Description

By putting all the output from the getDescriptionStatsBy into a list, naming each element that we want in an rgroup we can automatically merge everything and create an object ready for the htmlTable.

Usage

1
mergeDesc(..., htmlTable_args = list(css.rgroup = ""))

Arguments

...

One or more elements coming from getDescriptionStatsBy. You can also provide pure output from the getDescriptionStatsBy function and have the function merge this together with the ... argument. Note that all elements myst have the same by argument or you will not be able to merge it into a list.

htmlTable_args

Any arguments that should be passed to htmlTable function. The default is to remove any css formatting for the rgroup.

Value

matrix Returns a matrix object of class descList

The rgroup value

The value for the rgroup is by default the name of the list element. If you have passed a list without a name for that particular element or if you have passed a matrix it will look for a label set by the Hmisc::label function. For those elements that have only one row no rgroup is set, and the naming sequence is the same as above but with an additional rownames if the previous two turn out empty. All this behaviour is exemplified in the example.

The rgroup value can be overridden by simply specifying a custom rgroup when calling the htmlTable function.

The colnames of the matrix

The function chooses the colnames from the first element in the tlist.

Examples

 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
data(mtcars)
# For labelling we use the label()
# function from the Hmisc package
library(Hmisc)

label(mtcars$mpg) <- "Gas"
units(mtcars$mpg) <- "Miles/(US) gallon"

label(mtcars$wt) <- "Weight"
units(mtcars$wt) <- "10<sup>3</sup> kg" # not sure the unit is correct

mtcars$am <- factor(mtcars$am, levels=0:1, labels=c("Automatic", "Manual"))
label(mtcars$am) <- "Transmission"

mtcars$gear <- factor(mtcars$gear)
label(mtcars$gear) <- "Gears"

# Make up some data for making it slightly more interesting
mtcars$col <- factor(sample(c("red", "black", "silver"),
                     size=NROW(mtcars), replace=TRUE))
label(mtcars$col) <- "Car color"

mergeDesc(getDescriptionStatsBy(mtcars$mpg, mtcars$am,
                                header_count = TRUE,
                                use_units = TRUE),
          getDescriptionStatsBy(mtcars$wt, mtcars$am,
                                header_count = TRUE,
                                use_units = TRUE),
          htmlTable_args = list(caption  = "Basic continuous stats from the mtcars dataset"))

tll <- list()
tll[["Gear (3 to 5)"]] <- getDescriptionStatsBy(mtcars$gear, mtcars$am)
tll <- c(tll,
         list(getDescriptionStatsBy(mtcars$col, mtcars$am)))

mergeDesc(tll,
          htmlTable_args = list(caption  = "Factored variables"))

tl_no_units <- list()
tl_no_units[["Gas (mile/gallons)"]] <-
  getDescriptionStatsBy(mtcars$mpg, mtcars$am,
                        header_count = TRUE)
tl_no_units[["Weight (10<sup>3</sup> kg)"]] <-
  getDescriptionStatsBy(mtcars$wt, mtcars$am,
                        header_count = TRUE)
mergeDesc(tl_no_units, tll,
          # Remove the formatting for the groups
          htmlTable_args = list(css.rgroup = ""))


# A little more advanced
mtcars$mpg[sample(1:NROW(mtcars), size=5)] <- NA
getDescriptionStatsBy(mtcars$mpg, mtcars$am, statistics=TRUE)

# Do the horizontal version
getDescriptionStatsBy(mtcars$col, mtcars$am,
                      statistics=TRUE, hrzl_prop = TRUE)

mtcars$wt_with_missing <- mtcars$wt
mtcars$wt_with_missing[sample(1:NROW(mtcars), size=8)] <- NA
getDescriptionStatsBy(mtcars$wt_with_missing, mtcars$am, statistics=TRUE,
                      hrzl_prop = TRUE, total_col_show_perc = FALSE)


mtcars$col_with_missing <- mtcars$col
mtcars$col_with_missing[sample(1:NROW(mtcars), size=5)] <- NA
getDescriptionStatsBy(mtcars$col_with_missing, mtcars$am, statistics=TRUE,
                      hrzl_prop = TRUE, total_col_show_perc = FALSE)


## Not run: 
  ## There is also a LaTeX wrapper
  tll <- list(
    getDescriptionStatsBy(mtcars$gear, mtcars$am),
    getDescriptionStatsBy(mtcars$col, mtcars$am))

  latex(mergeDesc(tll),
        caption  = "Factored variables",
        file="")

## End(Not run)

Example output

Loading required package: Rcpp
Loading required package: htmlTable
Loading required package: lattice
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2

Attaching package: 'Hmisc'

The following objects are masked from 'package:base':

    format.pval, round.POSIXt, trunc.POSIXt, units

<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
<thead>
<tr><td colspan='4' style='text-align: left;'>
Basic continuous stats from the mtcars dataset</td></tr>
<tr>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'> </th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Automatic<br />
 No. 19</th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Manual<br />
 No. 13</th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>units</th>
</tr>
</thead>
<tbody>
<tr>
<td style='text-align: left;'>Gas</td>
<td style='text-align: center;'>17.1 (&plusmn;3.8)</td>
<td style='text-align: center;'>24.4 (&plusmn;6.2)</td>
<td style='text-align: center;'>Miles/(US) gallon</td>
</tr>
<tr>
<td style='border-bottom: 2px solid grey; text-align: left;'>Weight</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>3.8 (&plusmn;0.8)</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>2.4 (&plusmn;0.6)</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>10<sup>3</sup> kg</td>
</tr>
</tbody>
</table>
<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
<thead>
<tr><td colspan='3' style='text-align: left;'>
Factored variables</td></tr>
<tr>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'> </th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Automatic</th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Manual</th>
</tr>
</thead>
<tbody> 
<tr><td colspan='3' style='font-weight: 900;'>Gear (3 to 5)</td></tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;3</td>
<td style='text-align: center;'>15 (78.9%)</td>
<td style='text-align: center;'>0 (0.0%)</td>
</tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;4</td>
<td style='text-align: center;'>4 (21.1%)</td>
<td style='text-align: center;'>8 (61.5%)</td>
</tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;5</td>
<td style='text-align: center;'>0 (0.0%)</td>
<td style='text-align: center;'>5 (38.5%)</td>
</tr> 
<tr><td colspan='3' style='font-weight: 900;'>Car color</td></tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;black</td>
<td style='text-align: center;'>10 (52.6%)</td>
<td style='text-align: center;'>1 (7.7%)</td>
</tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;red</td>
<td style='text-align: center;'>5 (26.3%)</td>
<td style='text-align: center;'>6 (46.2%)</td>
</tr>
<tr>
<td style='border-bottom: 2px solid grey; text-align: left;'>&nbsp;&nbsp;silver</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>4 (21.1%)</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>6 (46.2%)</td>
</tr>
</tbody>
</table>
<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
<thead>
<tr>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'> </th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Automatic<br />
 No. 19</th>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Manual<br />
 No. 13</th>
</tr>
</thead>
<tbody>
<tr>
<td style='text-align: left;'>Gas (mile/gallons)</td>
<td style='text-align: center;'>17.1 (&plusmn;3.8)</td>
<td style='text-align: center;'>24.4 (&plusmn;6.2)</td>
</tr>
<tr>
<td style='text-align: left;'>Weight (10<sup>3</sup> kg)</td>
<td style='text-align: center;'>3.8 (&plusmn;0.8)</td>
<td style='text-align: center;'>2.4 (&plusmn;0.6)</td>
</tr> 
<tr><td colspan='3' style=''>Gear (3 to 5)</td></tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;3</td>
<td style='text-align: center;'>15 (78.9%)</td>
<td style='text-align: center;'>0 (0.0%)</td>
</tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;4</td>
<td style='text-align: center;'>4 (21.1%)</td>
<td style='text-align: center;'>8 (61.5%)</td>
</tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;5</td>
<td style='text-align: center;'>0 (0.0%)</td>
<td style='text-align: center;'>5 (38.5%)</td>
</tr> 
<tr><td colspan='3' style=''>Car color</td></tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;black</td>
<td style='text-align: center;'>10 (52.6%)</td>
<td style='text-align: center;'>1 (7.7%)</td>
</tr>
<tr>
<td style='text-align: left;'>&nbsp;&nbsp;red</td>
<td style='text-align: center;'>5 (26.3%)</td>
<td style='text-align: center;'>6 (46.2%)</td>
</tr>
<tr>
<td style='border-bottom: 2px solid grey; text-align: left;'>&nbsp;&nbsp;silver</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>4 (21.1%)</td>
<td style='border-bottom: 2px solid grey; text-align: center;'>6 (46.2%)</td>
</tr>
</tbody>
</table>
Gas 
          Automatic            Manual               P-value
Mean (SD) "16.9 (&plusmn;3.8)" "22.2 (&plusmn;5.1)" "0.016"
Missing   "1 (5.3%)"           "4 (30.8%)"          ""     
Car color 
       Total        Automatic    Manual      P-value
black  "11 (34.4%)" "10 (90.9%)" "1 (9.1%)"  "0.028"
red    "11 (34.4%)" "5 (45.5%)"  "6 (54.5%)" ""     
silver "10 (31.2%)" "4 (40.0%)"  "6 (60.0%)" ""     
Weight 
          Total               Automatic           Manual              P-value
Mean (SD) "3.2 (&plusmn;1.0)" "3.7 (&plusmn;0.8)" "2.5 (&plusmn;0.7)" "0.001"
Missing   "8"                 "5 (26.3%)"         "3 (23.1%)"         ""     
Car color 
        Total Automatic    Manual      P-value
black   "9"   "8 (88.9%)"  "1 (11.1%)" "0.020"
red     "10"  "4 (40.0%)"  "6 (60.0%)" ""     
silver  "8"   "2 (25.0%)"  "6 (75.0%)" ""     
Missing "5"   "5 (100.0%)" "0 (0.0%)"  ""     
%latex.default(mergeDesc(tll), caption = "Factored variables",     file = "", rgroup = c("Gears", "Car color"), n.rgroup = c(3L,     3L))%
\begin{table}[!tbp]
\caption{Factored variables\label{x}} 
\begin{center}
\begin{tabular}{lll}
\hline\hline
\multicolumn{1}{l}{x}&\multicolumn{1}{c}{Automatic}&\multicolumn{1}{c}{Manual}\tabularnewline
\hline
{\bfseries Gears}&&\tabularnewline
~~3&15 (78.9\%)&0 (0.0\%)\tabularnewline
~~4&4 (21.1\%)&8 (61.5\%)\tabularnewline
~~5&0 (0.0\%)&5 (38.5\%)\tabularnewline
\hline
{\bfseries Car color}&&\tabularnewline
~~black&10 (52.6\%)&1 (7.7\%)\tabularnewline
~~red&5 (26.3\%)&6 (46.2\%)\tabularnewline
~~silver&4 (21.1\%)&6 (46.2\%)\tabularnewline
\hline
\end{tabular}\end{center}
\end{table}

Gmisc documentation built on April 22, 2018, 1:04 a.m.