2

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

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 (i.e. total cases reflects the number of observations that have at least one choice in the mdset.

emudrak
  • 789
  • 8
  • 25

1 Answers1

2

I figured it out while looking at the examples for non mdset tables:

  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 |
emudrak
  • 789
  • 8
  • 25