【问题标题】:How to put standard error below estimate in a table in R, and transfer to flextable如何在 R 中的表中将标准误差低于估计值,并转移到 flextable
【发布时间】:2020-08-29 17:34:31
【问题描述】:

我正在使用 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 

谢谢!

【问题讨论】:

    标签: r datatable calculated-columns survey flextable


    【解决方案1】:

    类似的东西:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 2013-06-03
      • 2018-05-27
      • 1970-01-01
      • 2018-02-01
      • 2021-02-28
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多