Problems with htmlTable

Good evening,

I would like to ask you if it would be possible to separate in this table the variables sex and age into their categories. I mean, leaving the main row with total sex and all the ages and then creating on the same table one row for the interval (10,20] and (20,30] and for "male" and "female".

Would it be possible? Thank you again I would really appreciate it if you could help me.

library(reprex)
#> Warning: package 'reprex' was built under R version 3.6.3
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
library(Hmisc)
#> Warning: package 'Hmisc' was built under R version 3.6.3
#> Loading required package: lattice
#> Loading required package: survival
#> Loading required package: Formula
#> 
#> Attaching package: 'Hmisc'
#> The following objects are masked from 'package:dplyr':
#> 
#>     src, summarize
#> The following objects are masked from 'package:base':
#> 
#>     format.pval, units
library(Gmisc)
#> Warning: package 'Gmisc' was built under R version 3.6.3
#> Loading required package: Rcpp
#> Loading required package: htmlTable
#> Warning: package 'htmlTable' was built under R version 3.6.3
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(rstudioapi)
#> Warning: package 'rstudioapi' was built under R version 3.6.3
library(htmlTable)
library(readr)

essai <- read_csv("C:/Users/juanp/Desktop/R4SARS/Sources/essai.csv")
#> Parsed with column specification:
#> cols(
#>   Nom = col_character(),
#>   Sex = col_double(),
#>   Age = col_double(),
#>   Death = col_double()
#> )

essai<-essai%>%mutate(Sex=factor(Sex,labels = c("Male","Female")))
essai<-essai%>%mutate(age_cat=cut(Age,c(10,20,30)))

as.vector(essai)
#> # A tibble: 13 x 5
#>    Nom     Sex      Age Death age_cat
#>    <chr>   <fct>  <dbl> <dbl> <fct>  
#>  1 Juan    Male      14     1 (10,20]
#>  2 Juan    Female    21     0 (20,30]
#>  3 Maria   Male      14     1 (10,20]
#>  4 Ale     Female    25     1 (20,30]
#>  5 Fra     Male      16     0 (10,20]
#>  6 Helena  Male      21     0 (20,30]
#>  7 Roger   Female    16     0 (10,20]
#>  8 Beckham Female    23     0 (20,30]
#>  9 USMLE   Male      19     0 (10,20]
#> 10 Paul    Male      17     0 (10,20]
#> 11 Pauline Female    15     0 (10,20]
#> 12 Selena  Female    29     1 (20,30]
#> 13 Trump   Female    25     0 (20,30]



getTable1Stats <- function(x, digits=1){
  getDescriptionStatsBy(x,
                        by=essai$Death,
                        digits=digits,
                        continuous_fn = describeMedian,
                        useNA = "ifany",
                        factor_fn = describeFactors,
                        header_count=TRUE, 
                        add_total_col="first",
                        statistics = list(continuous = getPvalKruskal, 
                                          factor = getPvalChiSq, 
                                          proportion = getPvalFisher),
                        NEJMstyle = TRUE)
}



Table1 <- mergeDesc("age"=getTable1Stats(essai$age_cat),
                    "sex"=getTable1Stats(essai$Sex))



htmlTable(Table1)

Total

No. 13

0

No. 9

1

No. 4

P-value

age

7 (53.8%)

5 (55.6%)

2 (50.0%)

1.0

sex

6 (46.2%)

4 (44.4%)

2 (50.0%)

1.0

Created on 2020-05-08 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.