Search code examples
rdataframedplyrsummarize

R code to group multicode question responses by


I have a dataframe containing responses to a multicode question by month, where:

  • 1 = Yes;
  • 0 = No.

The output I'm after is a contingency table (with %s) detailing the proportion of 1 ('Yes') responses by month for each of the multicode columns:

Example data:

| Month | Multicode_1| .... | Multicode_n|
| ---   | .......... | .... | .......... |
| Jan22 |      1     | .... | .......... |
| Feb22 |      0     | .... | .......... |

Feels like this almost gets to it, but not quite:

ex8 <- NPSSurvey_df %>%
  group_by(Month) %>% filter(Pack == "Sports", CustomerType == "New") %>%
  summarise(across(Vid_Freeze:Mistake))
head(ex8)

Solution

  • Here an example with some simulated data, since the data is binary the mean will be the same as the proportion of 1's.

    library(dplyr)
    
        
    data <-
      data.frame(
        month = sample(month.abb,100,replace = TRUE),
        multicode1 = rbinom(100,1,.50),
        multicode2 = rbinom(100,1,.15),
        multicode3 = rbinom(100,1,.25),
        multicode4 = rbinom(100,1,.85)
        )
    
    data %>% 
      group_by(month) %>% 
      summarise(across(.fns = ~100*mean(.,na.rm = TRUE)))