简体   繁体   English

带有计数和百分比的 expss mdset 表

[英]expss mdset table with both counts and percentages

For a multiple response (multiple dichotomy) set, I would like to make a simple table showing both counts and percents of valid responses.对于多重响应(多重二分法)集,我想制作一个简单的表格,显示有效响应的计数和百分比。

checkall <- data.frame(ID=1:60, 
                     q1a=sample(c(0,1), size=60, replace=TRUE),
                     q1b=sample(c(0,1), size=60, replace=TRUE),
                     q1c=sample(c(0,1), size=60, replace=TRUE), 
                     q1d=sample(c(0,1), size=60, replace=TRUE),
                     q1e=sample(c(0,1), size=60, replace=TRUE),
                     q1f=sample(c(0,1), size=60, replace=TRUE),
                     q1g=sample(c(0,1), size=60, replace=TRUE),
                     q1h=sample(c(0,1), size=60, replace=TRUE))



calc_cro_cpct(checkall, mdset(q1a %to% q1h))

 |              | #Total |
 | ------------ | ------ |
 |          q1a |   53.3 |
 |          q1b |   48.3 |
 |          q1c |   43.3 |
 |          q1d |   43.3 |
 |          q1e |   41.7 |
 |          q1f |   55.0 |
 |          q1g |   63.3 |
 |          q1h |   48.3 |
 | #Total cases |   60.0 |

calc_cro_cases(checkall, mdset(q1a %to% q1h))

                         
 |              | #Total |
 | ------------ | ------ |
 |          q1a |     32 |
 |          q1b |     29 |
 |          q1c |     26 |
 |          q1d |     26 |
 |          q1e |     25 |
 |          q1f |     33 |
 |          q1g |     38 |
 |          q1h |     29 |
 | #Total cases |     60 |

Is there a way to get these into the same table, side by side?有没有办法将这些并排放在同一张桌子上?

I managed to make it work with tidyverse (without a total cases row).我设法让它与tidyverse工作(没有总案例行)。

library(tidyverse)
checkall %>% 
     select(q1a:q1h) %>% 
     summarize_all(sum, na.rm=TRUE) %>% 
     pivot_longer(everything(), names_to="Choice", values_to = "count") %>% 
     mutate(percent=round(count/nrow(checkall), 3))

# A tibble: 8 x 3
  Choice count percent
  <chr>  <dbl>   <dbl>
1 q1a       32   0.533
2 q1b       29   0.483
3 q1c       26   0.433
4 q1d       26   0.433
5 q1e       25   0.417
6 q1f       33   0.55 
7 q1g       38   0.633
8 q1h       29   0.483

But I'd like to see if expss can do it because if the ease in computing valid percent (ie total cases reflects the number of observations that have at least one choice in the mdset .但我想看看expss可以做到这一点,因为如果计算有效百分比(即总案例数反映了在mdset中至少有一个选择的观察数量)。

I figured it out while looking at the examples for non mdset tables:我在查看mdset 表的示例时发现了这一点:

  checkall %>% tab_cells(mdset(q1a %to% q1h)) %>%
    #tab_cols(q11) %>%
    tab_stat_cases(label = "freq") %>%
    tab_stat_cpct(label = "col %") %>%
    tab_pivot(stat_position = "inside_columns")

 |              | #Total |       |
 |              |   freq | col % |
 | ------------ | ------ | ----- |
 |          q1a |     32 |  53.3 |
 |          q1b |     29 |  48.3 |
 |          q1c |     26 |  43.3 |
 |          q1d |     26 |  43.3 |
 |          q1e |     25 |  41.7 |
 |          q1f |     33 |  55.0 |
 |          q1g |     38 |  63.3 |
 |          q1h |     29 |  48.3 |
 | #Total cases |     60 |  60.0 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM