Search code examples
rconcatenationdecimaldigitsrbind

rbind output formatting into publishable table


I used this this code

outcomes_all<- round (rbind(RD_enbloc, Additional.surgery, Procedure.time,Hospital.LOS,
                                 Negative.margin, Positive.margin, 
                              Vertical.margin ), digits=3); outcomes_all

and I got the following results that I used them to produce the next table :

     [,1] [,2]  [,3]   [,4]   [,5]   [,6]  [,7]  [,8]
[1,]   16 4536 0.271  0.161  0.381 96.254 0.000 0.000
[2,]   10  804 1.228  0.936  1.521 65.472 0.002 0.000
[3,]    2   63 1.232  0.681  1.783  0.000 0.831 0.000
[4,]    3  407 2.567  0.565 11.661 83.288 0.003 0.222
[5,]    3  407 0.443  0.229  0.855  0.000 0.617 0.015
[6,]    2  149 4.117  0.814 20.815 48.030 0.165 0.087

Code to remake this data:

df <- cbind(c(16, 10, 2, 3, 3, 2), 
            c(4536, 804, 63, 407, 407, 149),
            c(0.271, 1.228, 1.232, 2.567, 0.443, 4.117),
            c(0.161, 0.936, 0.681, 0.565, 0.229, 0.814),
            c(0.381, 1.521, 1.783, 11.661, 0.855, 20.815),
            c(96.254, 65.472, 0.000, 83.288, 0.000, 48.030),
            c(0.000, 0.002, 0.831, 0.003, 0.617, 0.165),
            c(0.000, 0.000, 0.000, 0.222, 0.015, 0.087))

Is there any proper way to get the final table 1 (image below) or better table 2 (image below; concatenatation of effect estimate, low and high CI columns and makning them only 2 decimals) automatically as an R output; basically rename columns and rows.

Table 1

enter image description here

Table 2

enter image description here

Any advice will be greatly appreciated.


Solution

  • You did not precise the format so here are several solutions to create table 1 (I think table 2 needs more manipulations). Some solutions were taken from here and you can find many other answers on the Internet:

    library(xtable)
    library(htmlTable)
    library(officer)
    library(flextable)
    library(magrittr)
    
    df <- cbind(c(16, 10, 2, 3, 3, 2), 
                c(4536, 804, 63, 407, 407, 149),
                c(0.271, 1.228, 1.232, 2.567, 0.443, 4.117),
                c(0.161, 0.936, 0.681, 0.565, 0.229, 0.814),
                c(0.381, 1.521, 1.783, 11.661, 0.855, 20.815),
                c(96.254, 65.472, 0.000, 83.288, 0.000, 48.030),
                c(0.000, 0.002, 0.831, 0.003, 0.617, 0.165),
                c(0.000, 0.000, 0.000, 0.222, 0.015, 0.087))
    
    df <- round(df, digits = 2)
    colnames(df) <- c("Studies", "patients", "Effect estimate", "Lower CI", "Upper CI", "I^2", "Heterogeneity p value", "Overall effect p value")
    rownames(df) <- c("En-bloc resection", "Procedure.time", "Hospital.LOS", "Negative margin", "Positive margin", "vertical margin")
    
    # LaTeX format
    xtable(df)
    
    ## HTML format
    htmlTable(df)
    
    ## CSV format (precise your path and the name of the file you want to create)
    write.csv(df)
    
    ## Word format:
    # Create flextable object
    ft <- flextable(data = as.data.frame(df)) %>% 
      theme_zebra %>% 
      autofit
    ft
    # Create a temp file
    tmp <- tempfile(fileext = ".docx")
    # Create a docx file
    read_docx() %>% 
      body_add_flextable(ft) %>% 
      print(target = tmp)
    
    # open word document
    browseURL(tmp)
    

    For more elaborated tables in LaTeX, you should look at the stargazer package.