rbind 输出格式为可发布 table

rbind output formatting into publishable table

我使用了这个代码

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

我得到了以下结果,我用它们来生成下一个 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

重制此数据的代码:

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))

是否有任何正确的方法来获得最终的 table 1(下图)或更好的 table 2(下图;效果估计的串联,低和高 CI 列并使它们只有 2 位小数)自动作为 R 输出; 基本上重命名列和行。

Table 1

Table 2

任何建议将不胜感激。

你没有精确的格式所以这里有几个创建 table 1 的解决方案(我认为 table 2 需要更多的操作)。一些解决方案来自 here,您可以在 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)

对于 LaTeX 中更详尽的 tables,您应该查看 stargazer 包。

两种选择都可以。 Table 1 是 Table 2 的前身,所以这里有两种解决方案。

这变得稍微困难​​一些,因为您的数据需要被强制转换为可用格式。您的数据已导入到 CSV 文件中,然后作为名为 "data" 的数据帧读入 R。您可以跳过这一步,因为您的数据已经在 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")

对于 Table 2,我通过 dplyr mutate 传递数据以组合列值以进行效果估计

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")

这会提供一个 table 准备好传递给另一个包进行格式化。

Kable、KableExtra、GT 都是不错的选择。 KableExtra 可以输出到 Latex 以供发布准备 tables.