rsurvey

R function to display if respondent answered yes to multiple choices


I have responses to a survey where a respondent was asked to pick all options that apply. Each choice is treated like its own variable - where if you selected yes to the multiple-choice option they are given a 1 and if not selected they are given a 0.

I want to see if someone selected an option how many also picked option B or option c. Ideally, I want to create a table that shows the percentage of people who answered yes (01) to option MR1 and then also answered yes to option MR2 and MR3.

df <- read.table(text = "ID MR1 MR2 MR3
Person1 1 1 1
Person2 1 0 0
Person3 1 1 0", header = T, sep = " ", stringsAsFactors = F)

Ideally, I would like the output to look something like this - showing 100% of people who selected MR1 also selected MR1, 2/3 of people who selected MR1 also picked MR2 or MR3, and 1/3 of people who selected MR2 also picked MR3 -- this will be helpful to see if there are any MR options that are more likely to be grouped together than others.

Is there any code for something like this?

       MR1  MR2 MR3
MR1    1    0.66    0.66   
MR2   0.66    1    0.33  
MR3   0.66    0.33   1

Solution

  • Here is one approach, with a bit different representation of results. In your case, you are only looking at pairs and not at all options that might be there.

    df <- df %>% mutate(x = case_when(MR1 == 0 ~ "none",
                                MR1 == 1 & MR2 == 0 & MR3 == 0 ~ "one",
                                MR1 == 1 & MR2 == 1 & MR3 == 0 ~ "two-second",
                                MR1 == 1 & MR2 == 0 & MR3 == 1 ~ "two-third",
                                MR1 == 1 & MR2 == 1 & MR3 == 1 ~ "three"))
    
    df %>% group_by(x) %>% summarise(answers = n()) %>% mutate(share = round(answers/sum(answers)*100))