将输出格式格式化到可发布的表中

问题描述 投票:0回答:1

我使用了此代码

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

并且我得到了以下结果,我用它们来产生下表:

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

是否有任何适当的方法自动获得R表的最终表1(下图)或更好的表2(下图;效果估计值的串联,低和高CI列并将它们仅保留两个小数);基本上重命名列和行。

表1

enter image description here

表2

enter image description here

任何建议将不胜感激。

r concatenation decimal digits rbind
1个回答
0
投票

您没有精确调整格式,因此这里有几种创建表1的解决方案(我认为表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中更详细的表,您应该查看stargazer包。

© www.soinside.com 2019 - 2024. All rights reserved.