问题描述
我正在使用apistrat数据创建汇总统计表,并将输出作为R表创建(希望最终使用flextable格式化)。我希望对表格进行操作,以使每个变量的标准误差都直接显示在我收集的均值/中值下方。这是一个示例:
dstrata <- apistrat %>%
as_survey_design(strata = stype,weights = pw)
dstrata <- dstrata %>%
mutate(api_diff = api00 - api99)
dstrata %>%
summarise(api_diff = survey_mean(api_diff,vartype="se" ))
api_diff api_diff_se
<dbl> <dbl>
1 32.9 2.08
#so as you can see now,the standard error appears as its own column. Is there a way to reformat the table so it appears like this?
api_diff
<dbl>
1 32.9
(2.08)
一旦采用这种形式,我如何才能将其转置为可弯曲表格?每当我尝试这样做时,都会出现以下错误:
> table=flextable(dstrata)
Error in flextable(dstrata) : is.data.frame(data) is not TRUE
谢谢!
解决方法
有点像:
library(flextable)
library(srvyr)
library(survey)
library(dplyr)
data(api)
# stratified sample
dstrata <- apistrat %>%
as_survey_design(strata = stype,weights = pw)
dstrata <- dstrata %>%
mutate(api_diff = api00 - api99)
dat <- dstrata %>%
summarise(api_diff = survey_mean(api_diff,vartype="se" ))
flextable(dat,col_keys = "api_diff") %>%
compose(j = 1,value = as_paragraph(api_diff," (",api_diff_se,")")) %>%
autofit()