2

我想做一个性别和疾病之间的列联表。当我将 R.markdown 用于 pdf 时。报告,我用kableExtra来自定义表格。当它们不是 data.frame 时,KableExtra 不能很好地制作表格。所以他们用tableby

有了这个data.frame,这就是我得到的。

library(kableExtra)
library(arsenal)
set.seed(0)
Disease<-sample(c(rep("Name of the first category of the disease",20),
rep("Name of the Second category of the disease",32),
rep("Name of the third category of the disease",48),
rep("The category of those who do not belong to the first three categories",13)))
ID<-c(1:length(Disease))
Gender<-rbinom(length(Disease),1,0.55)
Gender<-factor(Gender,levels = c(0,1),labels = c("F","M"))

data<-data.frame(ID,Gender,Disease)

当我用 R.markdown (pdf) 运行这个分析的结果时,我得到了这种表

在此处输入图像描述

有两个问题,渴KableExtra::不处理字符其次,当我使用with时,&nbsp;&nbsp;&nbsp; 我无法自定义列宽,因为我想放大包含变量名的列,因为我真的在处理名称的数据变量值很长。但是如果我使用,字符会被删除,但表格不会按比例缩小,并且不会显示一部分。我认为有很多限制。tablebykableExtrakableknitr::&nbsp;&nbsp;&nbsp;knitr

我该如何处理这个问题?或者是否有另一个函数可以在 R.markdown(pdf 格式)中使用来制作带有 p.value 的漂亮列联表。

4

1 回答 1

2

为避免&nbsp;&nbsp;&nbsp;在使用 knitr 时将其添加到输出中,请在块设置选项中使用 results='asin' ,例如:

{r results='asis'}

您可以使用 print 函数中的 width 选项来控制包含变量名的列的宽度。从技术上讲,您不需要将摘要调用包装在 print 中,但如果您这样做,它不会改变任何东西,并且您可以调整宽度设置。

因此,对于您的示例:

print(summary(tableby(Gender~Disease)), width = 20)

应该使它在呈现为 pdf 时更具可读性。您可以更改宽度,它将在您设置的限制处换行。

使用示例中的代码和上述函数调用,表格在 knit 为 pdf 时如下所示:

在此处输入图像描述

于 2020-07-17T21:54:27.370 回答