-2

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)
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • 4
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with—this amount of data doesn't make it clear what you're working with, and you haven't included what this gets you or what you're trying to get exactly. You're most likely right that it's been asked & answered, but right now it's not clear enough to find a previous post – camille Dec 16 '22 at 16:44
  • 1
    Hello Andrew and welcome to SO! First, I recomend you to read this quiestion about how to make a great reproductible example: stackoverflow.com/questions/5963269/…. Always try to post some example data, so that others can replicate what you are doing. – Santiago Capobianco Dec 16 '22 at 16:45
  • You need a function in your summarise - e.g. something like `summarise(across(Vid_Freeze:Mistake, ~sum(. == 1) / n())`, which counts the 1s and divides by the number of members of each group. – Andrew Gustar Dec 16 '22 at 16:54

1 Answers1

0

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)))
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32