title: How to group comma-separated variables in the same column?
body: <p>Here is my false data:</p>
  <pre><code>#&gt; id   column                 
  #&gt; 1    blue, red, dog, cat
  #&gt; 2    red, blue, dog
  #&gt; 3    blue      
  #&gt; 4    red
  #&gt; 5    dog, cat   
  #&gt; 6    cat
  #&gt; 7    red, cat
  #&gt; 8    dog
  #&gt; 9    cat, red
  #&gt; 10   blue, cat
  </code></pre>
  <p>I want to tell R for example that <code>dog and cat = animal</code> and <code>red and blue = colour</code>. I want to basically count the number (and eventually percentage) of animals, colours and both.</p>
  <pre><code>#&gt; id   column                 newcolumn
  #&gt; 1    blue, red, dog, cat    both
  #&gt; 2    red, blue, dog         both
  #&gt; 3    blue                   colour
  #&gt; 4    red                    colour
  #&gt; 5    dog, cat               animal
  #&gt; 6    cat                    animal
  #&gt; 7    red, cat               both
  #&gt; 8    dog                    animal
  #&gt; 9    cat, red               both
  #&gt; 10   blue, cat              both
  </code></pre>
  <p>So far I have only been able to total up the number of red, blue, dog and cat by doing the following:</p>
  <pre><code>column.string&lt;-paste(df$column, collapse=&quot;,&quot;)
  column.vector&lt;-strsplit(column.string, &quot;,&quot;)[[1]]
  column.vector.clean&lt;-gsub(&quot; &quot;, &quot;&quot;, column.vector)
  table(column.vector.clean)
  </code></pre>
  <p>Would be very grateful for help, here is my sample false data:</p>
  <pre><code>df &lt;- data.frame(id = c(1:10), 
                   column = c(&quot;blue, red, dog, cat&quot;, &quot;red, blue, dog&quot;, &quot;blue&quot;, &quot;red&quot;, &quot;dog, cat&quot;, &quot;cat&quot;, &quot;red, cat&quot;, &quot;dog&quot;, &quot;cat, red&quot;, &quot;blue, cat&quot;))
  </code></pre>