微调 table 函数 'univariateTable' 的输出

Fine tuning table output of function 'univariateTable'

我发现函数 univariateTable 非常有助于处理更大的数据以获得漂亮、干净的 table 输出。但是在 csv 中导出 table 之后,我仍然需要手动执行一些操作,我宁愿在 R 中执行此操作以自动执行该过程并避免人为错误。

这是带有 table 输出的示例代码,然后我将其导出为 csv

value<-cbind(c(rnorm(103.251,503.24,90),rnorm(103.251,823.24,120)))
genotype<-cbind(c(rep("A",100),rep("B",100)))
gender<-rep(c("M","F","F","F"),50)
df<-cbind(value,genotype,gender)
df<-as.data.frame(df)
colnames(df)<-c("value","genotype","gender")
df$value<-as.numeric(as.character(df$value))
library(Publish)
summary(univariateTable(gender ~ Q(value) + genotype, data=df))

我遇到的两个问题是:

  1. 有没有一种方法可以像这样对 table 中的数字进行四舍五入:round(99.73)

  2. 有没有办法在四分位数范围输出中用 - 替换 , ,方式类似于: gsub(", ","-","[503.7, 793.3]") ,而不是 median [iqr] 放出来了 median [IQR]

同样,我在导出 tables 后手动执行这些操作,但对于较大的 tables,自动化该过程要方便得多。

univariateTable 有一个可用于四舍五入的数字参数。要修改格式,您可以检查 univariateTable 返回的列表以确定在哪里可以找到需要更改的值。

您的示例数据引发了错误,因此我对其进行了修改以使其成为 运行 并稍微清理了代码。

# devtools::install_github("tagteam/Publish")
library(Publish)

value <- c(rnorm(90, 103.251,503.24),rnorm(110, 103.251,823.24))
genotype <- rep(c("A","B"), each=100)
gender <- rep(c("M","F","F","F"),50)
df <- data.frame(value,genotype,gender)

univariateTabledigits 参数可用于舍入(有关函数的帮助信息,请参阅 ?univariateTable)。

tab = univariateTable(gender ~ Q(value) + genotype, data=df, digits=0)

要将逗号更改为连字符,我们需要查看这些值在 univariateTable 返回的列表中的存储位置。 运行 str(tab),向您展示了列表的结构。请注意 table 中的标题值看起来像是存储在 tab$summary.groups$valuetab$summary.totals$value 中,因此我们将编辑它们:

tab$summary.groups$value = gsub(", ", " - ", tab$summary.groups$value)
tab$summary.totals$value = gsub(", ", " - ", tab$summary.totals$value)

tab

  Variable        Level gender = F (n=150) gender = M (n=50)   Total (n=200) p-value
1    value median [iqr]    -6 [-481 - 424]  203 [-167 - 544] 80 [-433 - 458]   0.118
2 genotype            A            75 (50)           25 (50)        100 (50)        
3                     B            75 (50)           25 (50)        100 (50)   1.000