gpt4 book ai didi

rbind 输出格式到可发布表

转载 作者:行者123 更新时间:2023-12-01 06:16:42 35 4
gpt4 key购买 nike

我用过这段代码

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

是否有任何适当的方法可以自动将最终表 1(下图)或更好的表 2(下图;效果估计、低和高 CI 列的串联,并将它们设为 2 位小数)自 Action 为 R 输出;基本上重命名列和行。

表一

enter image description here

表2

enter image description here

任何建议将不胜感激。

最佳答案

您没有精确说明格式,因此这里有几种创建表 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 包。

关于rbind 输出格式到可发布表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922071/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com