提问人:Mohamed Rahouma 提问时间:11/19/2019 最后编辑:bretauvMohamed Rahouma 更新时间:11/19/2019 访问量:382
将 rbind 输出格式转换为可发布的表
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
我得到了以下结果,我用它们来生成下一张表:
[,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(下图;效应估计、低置信区间和高置信区间列的串联,并仅将它们精确到小数点后 2 位)作为 R 输出; 基本上是重命名列和行。
表1
表2
任何建议将不胜感激。
答:
4赞
bretauv
11/19/2019
#1
您没有精确格式,因此这里有几种创建表 1 的解决方案(我认为表 2 需要更多操作)。从这里获取了一些解决方案,您可以在 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
3赞
hisspott
11/19/2019
#2
这两种选择都是可能的。表 1 是表 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")
对于表 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")
然后,这将提供一个表,准备传递给另一个包进行格式化。
Kable、KableExtra、GT 都是不错的选择。KableExtra 可以输出到 latex 以获得可发布的表格。
评论