我正在使用R中tableContinuous包的reporttools函数。

一切正常,并使用docs中的示例:

library("reporttools")
data(CO2)
vars <- CO2[, 4:5]
group <- CO2[, "Treatment"]
weights <- c(rep(1, 60), rep(0, 10), rep(2, 14))

tableContinuous(vars = vars, weights = weights, subset =
                  c(rep(TRUE, 57), rep(FALSE, 100 - 57)), group = group, prec = 3, print.pval =
                  "kruskal", cap = "Table of continuous variables.", lab = "tab: descr stat")

我得到一张预期的桌子:

是否可以从输出中删除all行?

最佳答案

不可以,tableContinuous中没有这样的选项。仅在每个变量只有一个级别的情况下才省略all行,即,参见if (n.levels == 1) ...源代码末尾的tableContinuous

但是,可以使用正则表达式解决此问题。我不是专家,所以可能会有更好的方法。

library(reporttools)
data(CO2)
vars <- CO2[, 4:5]
group <- CO2[, "Treatment"]
weights <- c(rep(1, 60), rep(0, 10), rep(2, 14))

result <- tableContinuous(vars = vars, weights = weights, subset =
                            c(rep(TRUE, 57), rep(FALSE, 100 - 57)), group = group, prec = 3, print.pval =
                            "kruskal", cap = "Table of continuous variables.", lab = "tab: descr stat")

完全删除all行:
cat(gsub("\\\\hline\n[^\n]+& all &[^\n]+\n", "", result))

保持p值:
greg <- gregexpr("p (=|<) [^\n]+", result)
regmatches(result, greg) <- list(gsub("(?<=&)[-.\\w ]+", " ",
                                     regmatches(result, greg)[[1]], perl = TRUE))
cat(result)

10-06 03:19