1

I have created a table for cars and speed with gtsummary.

enter image description here

The column heading as shown is the cars and the row headings are the summary of the characteristics I was searching for to be in the table. I prefer the N, Median and Range to be the column heading (TOP) and the row heading to be the cars (At the SIDE). Can you help me change my code to suit that, Attached is a pic of the table

My Code

library(dplyr)
library(gtsummary)
library(flextable)
library(officer)

dff <- structure(list(car = c("Honda", "Opel", "Toyota", "Ford", "Toyota", 
                              "Toyota", "Toyota", "Toyota", "Toyota", "Toyota", "Opel", "Opel", 
                              "Opel", "Opel", "Opel", "Opel", "Opel", "Opel", "Opel", "Opel", 
                              "Opel", "Opel", "Opel", "Opel", "Opel", "Opel", "Ford", "Ford", 
                              "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", 
                              "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", 
                              "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", 
                              "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", "Honda", 
                              "Honda", "Honda", "Ford", "Ford", "Ford", "Ford", "Ford", "Ford", 
                              "Ford", "Ford", "Ford", "Ford", "Ford", "Ford"),
                      speed = c(0.0818884144530994, 
                               0.078202618461924, 0.0923996477945826, 0.0833895373090655, 0.0654271133558503, 
                               0.104902087082777, 0.0767497512256455, 0.0768176971270742, 0.0712864905867507, 
                               0.0554218586661056, 0.0850360296771161, 0.057954145633874, 0.0441854480859481, 
                               0.0881234075504796, 0.0639960050103843, 0.0620009991911811, 0.104363010616978, 
                               0.0794749032774448, 0.0639931924725228, 0.0347553634870904, 0.0235164440970578, 
                               0.000945735768549479, 0.0951332350399264, 0.0848849882638771, 
                               0.0770268456523483, 0.0860962939374158, 0.0779784304434212, 0.0700250314203401, 
                               0.0979442195442822, 0.0895676676419504, 0.145633658479367, 0.114931834231455, 
                               0.0907671090187226, 0.118083514719288, 0.0903243829523317, 0.0852324890285871, 
                               0.0196699224014573, 0.080910276397263, 0.0667256842832578, 0.100828213795925, 
                               0.0878058668694595, 0.0758022260504243, 0.106719838699154, 0.0920508745930191, 
                               0.0710548353544975, 0.0859097610562796, 0.0692502648434324, 0.0642848032824688, 
                               0.0934410581211051, 0.0901226640111047, 0.0880892316582102, 0.0526000795151807, 
                               0.0487590677497554, 0.0362328359734826, 0.130861058707153, 0.115286968138184, 
                               0.0973709227872183, 0.075845698962114, 0.0726245579593528, 0.0660583859152627, 
                               0.0432445861280246, 0.0326512563074741, 0.0833250468064319, 0.0712792343009829, 
                               0.0662704232419949, 0.0267930511166544, 0.0195822515826592, 0.0182287564631037, 
                               0.0565616222676817, 0.0462813673349305)),
                 class = "data.frame", row.names = c(NA, -70L))

#create the table
dff %>%
 tbl_summary(by = car,type = all_continuous() ~ "continuous2",
 label = list(speed ~ "Speed", car ~ "Car Type"),
 digits = all_continuous() ~ 3,
 statistic = all_continuous() ~ c("{N_nonmiss}",
 "{median} ({p25}, {p75})", 
 "{min}, {max}"),) %>%
 modify_header(all_stat_cols() ~ "**{level}**" )%>%
 modify_caption("Car according to Speed") %>%
 bold_labels()%>%
 add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
 bold_p()%>%
 as_flex_table()
Andrea M
  • 2,314
  • 1
  • 9
  • 27
Eugene E
  • 13
  • 3
  • In practice, this would be quite a complicated process. The `as_flex_table()` function outputs a complex flextable object that contains many embedded lists, tables, values, properties etc. Trying to transpose the correct values while keeping the format correct would be difficult and error prone. – JaredS Jun 22 '22 at 09:46

1 Answers1

1

It may be possible with gtsummary but I am not sure how. An alternative with dplyr/flextable only could be:

library(dplyr)
library(flextable)

dff <- data.frame(
  stringsAsFactors = FALSE,
  car = c("Honda","Opel","Toyota", "Ford","Toyota","Toyota","Toyota","Toyota","Toyota", "Toyota","Opel","Opel","Opel","Opel","Opel","Opel", "Opel","Opel","Opel","Opel","Opel","Opel","Opel", "Opel","Opel","Opel","Ford","Ford","Honda","Honda", "Honda","Honda","Honda","Honda","Honda","Honda", "Honda","Honda","Honda","Honda","Honda","Honda","Honda", "Honda","Honda","Honda","Honda","Honda","Honda", "Honda","Honda","Honda","Honda","Honda","Honda", "Honda","Honda","Honda","Ford","Ford","Ford","Ford", "Ford","Ford","Ford","Ford","Ford","Ford","Ford", "Ford"),
  speed = c(0.0818884144530994, 0.078202618461924,0.0923996477945826,0.0833895373090655, 0.0654271133558503,0.104902087082777,0.0767497512256455, 0.0768176971270742,0.0712864905867507,0.0554218586661056, 0.0850360296771161,0.057954145633874,0.0441854480859481, 0.0881234075504796,0.0639960050103843,0.0620009991911811, 0.104363010616978,0.0794749032774448, 0.0639931924725228,0.0347553634870904,0.0235164440970578, 0.000945735768549479,0.0951332350399264,0.0848849882638771, 0.0770268456523483,0.0860962939374158,0.0779784304434212, 0.0700250314203401,0.0979442195442822,0.0895676676419504, 0.145633658479367,0.114931834231455,0.0907671090187226, 0.118083514719288,0.0903243829523317,0.0852324890285871, 0.0196699224014573,0.080910276397263,0.0667256842832578, 0.100828213795925,0.0878058668694595, 0.0758022260504243,0.106719838699154,0.0920508745930191, 0.0710548353544975,0.0859097610562796,0.0692502648434324, 0.0642848032824688,0.0934410581211051,0.0901226640111047, 0.0880892316582102,0.0526000795151807,0.0487590677497554, 0.0362328359734826,0.130861058707153,0.115286968138184, 0.0973709227872183,0.075845698962114,0.0726245579593528, 0.0660583859152627,0.0432445861280246,0.0326512563074741, 0.0833250468064319,0.0712792343009829, 0.0662704232419949,0.0267930511166544,0.0195822515826592, 0.0182287564631037,0.0565616222676817,0.0462813673349305)
)

# do the calculations with dplyr and stats::kruskal.test

pvalue <- kruskal.test(dff$speed, g = dff$car )$p.value

dat <- dff %>% group_by(car) %>% 
  summarise(
    N_nonmiss = sum(!is.na(speed)),
    median = median(speed),
    p25 = quantile(speed, probs = .25),
    p75 = quantile(speed, probs = .75),
    min = min(speed, na.rm = TRUE),
    max = max(speed, na.rm = TRUE)
  )
  
# do the flextable
  
set_flextable_defaults(digits = 3)

flextable(dat, col_keys = c("car", "N", "Median (IQR)", "Range")) %>% 
  set_header_labels(
    "car" = "car", 
    "N" = "N", "Median (IQR)" = "Median (IQR)", "Range" = "Range") %>% 
  mk_par(j = "N",
         value = as_paragraph(N_nonmiss)) %>% 
  mk_par(j = "Median (IQR)",
         value = as_paragraph(median, " (", p25, ", ", p75,")")) %>% 
  mk_par(j = "Range",
         value = as_paragraph(min, ", ", max)) %>% 
  footnote(i = 1, j = "car", ref_symbols = "1", part = "header",
           value = as_paragraph(
             "p-value for Kruskal-Wallis rank sum test: ",
             as_chunk(pvalue, 
                      props = fp_text_default(bold = TRUE),
                      formatter = scales::pvalue))) %>% 
  align(align = c("left", "center", "center", "center"), part = "all") %>% 
  autofit() %>% 
  set_caption("Car according to Speed")

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34