Home > Enterprise >  Get rid of `N` using `modelsummary::datasummary_balance` in `R`
Get rid of `N` using `modelsummary::datasummary_balance` in `R`

Time:10-29

I'm using datasummary_balance and it works amazing...except it gives me the number of observations between treated and control. This is usually great but I have clusters so the number and the number of observations do not reflect the number of clusters. Any idea how to fix this?Troublesome N. Thanks in advance.

For clarification, I want to change the header on the table to not have N=...

CodePudding user response:

Unfortunately, there is currently no way to remove the number of observations in the column labels. The intention is for members of the datasummary_* family of functions to act as "templates", and for users to use the basic datasummary() function when they need to customize more extensively.

That said, depending on your output format, it can be trivial to remove the observation number using a simple regular expression.

LaTeX:

library(modelsummary)
library(magrittr)
dat = mtcars[, c("am", "hp", "mpg")]
dat$am = as.character(dat$am)

datasummary_balance(~am, output = "latex", data = dat) %>%
    gsub("\\(N=\\d*\\)", "", .)

# \begin{table}
# \centering
# \begin{tabular}[t]{lrrrrrr}
# \toprule
# \multicolumn{1}{c}{ } & \multicolumn{2}{c}{0 } & \multicolumn{2}{c}{1 } & \multicolumn{2}{c}{ } \\
# \cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-5}
#   & Mean & Std. Dev. & Mean  & Std. Dev.  & Diff. in Means & Std. Error\\
# \midrule
# hp & 160.3 & 53.9 & 126.8 & 84.1 & \num{-33.4} & \num{26.4}\\
# mpg & 17.1 & 3.8 & 24.4 & 6.2 & \num{7.2} & \num{1.9}\\
# \bottomrule
# \end{tabular}
# \end{table}

HTML:

datasummary_balance(~am, output = "html", data = dat) %>%
    gsub("\\(N=\\d*\\)", "", .)

# <table  style="width: auto !important; margin-left: auto; margin-right: auto;">
#  <thead>
# <tr>
# <th style="empty-cells: hide;border-bottom:hidden;" colspan="1"></th>
# <th style="border-bottom:hidden;padding-bottom:0; padding-left:3px;padding-right:3px;text-align: center; " colspan="2"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">0 </div></th>
# <th style="border-bottom:hidden;padding-bottom:0; padding-left:3px;padding-right:3px;text-align: center; " colspan="2"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">1 </div></th>
# <th style="empty-cells: hide;border-bottom:hidden;" colspan="2"></th>
# </tr>
#   <tr>
#    <th style="text-align:left;">   </th>
#    <th style="text-align:right;"> Mean </th>
#    <th style="text-align:right;"> Std. Dev. </th>
#    <th style="text-align:right;"> Mean  </th>
#    <th style="text-align:right;"> Std. Dev.  </th>
#    <th style="text-align:right;"> Diff. in Means </th>
#    <th style="text-align:right;"> Std. Error </th>
#   </tr>
#  </thead>
# <tbody>
#   <tr>
#    <td style="text-align:left;"> hp </td>
#    <td style="text-align:right;"> 160.3 </td>
#    <td style="text-align:right;"> 53.9 </td>
#    <td style="text-align:right;"> 126.8 </td>
#    <td style="text-align:right;"> 84.1 </td>
#    <td style="text-align:right;"> −33.4 </td>
#    <td style="text-align:right;"> 26.4 </td>
#   </tr>
#   <tr>
#    <td style="text-align:left;"> mpg </td>
#    <td style="text-align:right;"> 17.1 </td>
#    <td style="text-align:right;"> 3.8 </td>
#    <td style="text-align:right;"> 24.4 </td>
#    <td style="text-align:right;"> 6.2 </td>
#    <td style="text-align:right;"> 7.2 </td>
#    <td style="text-align:right;"> 1.9 </td>
#   </tr>
# </tbody>
# </table>
  • Related