Mohamed Rahouma
Mohamed Rahouma

Reputation: 1236

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.

Upvotes: 3

Views: 438

Answers (2)

hisspott
hisspott

Reputation: 476

Both options are possible. Table 1 is a precursor to Table 2 so here are both solutions.

This was made slightly more difficult as your data needed to be coerced into a usable format. Your data has been imported to a CSV then read into R as a dataframe called "data". You can skip this step as your data will already be in R.

library(tidyverse)
library(janitor) #Note Janitor is only used to make your column names usable in R.

data <- as.data.frame(read.csv(file = "so.csv", header = FALSE))

rownames <- list("RD_enbloc", "Procedure.time","Hospital.LOS","Negative.margin", "Positive.margin", "Vertical.margin")
rownames <- data.frame(matrix(unlist(rownames), nrow=length(rownames), byrow=T))
names(rownames) <- "procedure"

data <- as.data.frame(cbind(rownames, data))

colnames(data) <- c("Procedure", "Studies", "patients", "Effect estimate", "Lower CI", "Upper CI", "I^2", "Heterogenity p value", "Overall effect P value")

For Table 2 I pass the data through a dplyr mutate to combine the column values for your Effect estimate

data %>% clean_names() %>% 
  mutate(effect_estimate = round(effect_estimate, digits = 2),
     lower_ci = round(lower_ci, digits = 2), 
     upper_ci = round(upper_ci, digits = 2), 
combined_value = paste0(effect_estimate, " (95% CI = ", lower_ci, " - ", upper_ci, ")" )) %>% 
  select(procedure, studies, patients, combined_value, i_2, heterogenity_p_value, overall_effect_p_value, -effect_estimate, -lower_ci, -upper_ci) -> data

colnames(data) <- c(" ", "Studies", "patients", "Effect estimate (95% CI)", "I^2", "Heterogenity p value", "Overall effect P value")

This then provides a table ready to be passed to another package for formatting.

Kable, KableExtra, GT are all good options. KableExtra can output to latex for publication ready tables.

Upvotes: 3

bretauv
bretauv

Reputation: 8567

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.

Upvotes: 4

Related Questions