title: Analysis of multiple response
body: <pre><code>df1 &lt;-
    data.frame(c(&quot;male&quot;, &quot;female&quot;, &quot;male&quot;),
               c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;),
               seq(141, 170))
  
  names(df1) = c(&quot;gender&quot;, &quot;age&quot;, &quot;height&quot;)
  
  df1$age &lt;- factor(
    df1$age,
    levels = c(1, 2, 3, 4, 5, 6),
    labels = c(&quot;16-24&quot;, &quot;25-34&quot;, &quot;35-44&quot;, &quot;45-54&quot;, &quot;55-64&quot;, &quot;65+&quot;)
  )
  
  q1a = c(1, 0, 1, 0, 0, 1)
  q1b = c(0, 0, 2, 2, 2, 0)
  q1c = c(0, 0, 3, 3, 0, 3)
  # 1,2 and 3 used to be compatible with existing datasets. 
  # Could change all to 1 if necessary.
  
  df2 &lt;- data.frame(q1a = q1a, q1b = q1b, q1c = q1c)
  df1 &lt;- cbind(df1, df2)
  
  rm(q1a, q1b, q1c, df2)
  </code></pre>
  <p>I am looking to replicate the analysis of multiple response questions from SPSS in R.</p>
  <p>At the moment I am using this code:</p>
  <pre><code>#creating function for analysing questions with grouped data
  multfreqtable &lt;- function(a, b, c) {
    # number of respondents (for percent of cases)
    totrep = sum(a == 1 | b == 2 | c == 3)
    
    #creating frequency table
    table_a = data.frame(&quot;a&quot;, sum(a == 1))
    names(table_a) = c(&quot;question&quot;, &quot;freq&quot;)
    table_b = data.frame(&quot;b&quot;, sum(b == 2))
    names(table_b) = c(&quot;question&quot;, &quot;freq&quot;)
    table_c = data.frame(&quot;c&quot;, sum(c == 3))
    names(table_c) = c(&quot;question&quot;, &quot;freq&quot;)
    table_question &lt;- rbind(table_a, table_b, table_c)
    
    #remove individual question tables
    rm(table_a, table_b, table_c)
    
    #adding total
    total = as.data.frame(&quot;Total&quot;)
    totalsum = (sum(table_question$freq, na.rm = TRUE))
    totalrow = cbind(total, totalsum)
    names(totalrow) = c(&quot;question&quot;, &quot;freq&quot;)
    table_question = rbind(table_question, totalrow)
    
    #adding percentage column to frequency table
    percentcalc = as.numeric(table_question$freq)
    percent = (percentcalc / totalsum) * 100
    table_question &lt;- cbind(table_question, percent)
    
    #adding percent of cases column to frequency table
    poccalc = as.numeric(table_question$freq)
    percentofcases = (poccalc / totrep) * 100
    table_question &lt;- cbind(table_question, percentofcases)
    
    #print percent of cases value
    total_respondents &lt;&lt;- data.frame(totrep)
    
    #remove all unnecessary data and values
    rm(
      total,
      totalsum,
      totalrow,
      b,
      c,
      percent,
      percentcalc,
      percentofcases,
      totrep,
      poccalc
    )
    
    return(table_question)
  }
  
  #calling function - must tie to data.frame using $ !!!
  q1_frequency &lt;- multfreqtable(df1$q1a, df1$q1b, df1$q1c)
  
  #renaming percent of cases - This is very important while using current method
  total_respondents_q1 &lt;- total_respondents
  rm(total_respondents)
  </code></pre>
  <p>Producing this table as a result:</p>
  <p><img src="https://i.stack.imgur.com/NBm1n.png" alt="Output table" /></p>
  <p>I am looking for a more efficient method of doing this that ideally would not require the function to be edited if there were more or less multiple choice questions.</p>