title: Spread and sort concatenated factor variable in data.frame
body: <p>I have a <code>data.frame</code> that looks as follows:</p>
  
  <pre><code>df &lt;- data.frame(A = NA, B = NA, C = c("a,b,c", "c,b", "d,a"), stringsAsFactors = FALSE)
  df
     A  B     C
  1 NA NA a,b,c
  2 NA NA   c,b
  3 NA NA   d,a
  </code></pre>
  
  <p>Columns <code>A</code> and <code>B</code> (and some more in my real data) are set to <code>NA</code> to indicate that their entries are not necessary for the question.</p>
  
  <p>The goal is it should look like:</p>
  
  <pre><code>df_goal &lt;- data.frame(A = NA, B = NA, a = c(TRUE, FALSE, TRUE), b = c(TRUE, 
  TRUE, FALSE), c = c(TRUE, TRUE, FALSE), d = c(FALSE, FALSE, TRUE))
  df_goal
  
     A  B     a     b     c     d
  1 NA NA  TRUE  TRUE  TRUE FALSE
  2 NA NA FALSE  TRUE  TRUE FALSE
  3 NA NA  TRUE FALSE FALSE  TRUE
  </code></pre>
  
  <p>I achieved this doing:</p>
  
  <pre><code>df &lt;- cbind(df[, 1:2], as.data.frame(t(apply(read.table(text = df$C, sep = ",", as.is = TRUE, fill = TRUE, na.strings = "")
                                               , 1, 
                                               FUN = function(x) sort(x, decreasing= FALSE, na.last = TRUE))), stringsAsFactors = FALSE))
  df &lt;- cbind(df[, 1:2], as.data.frame(sapply(c("a", "b", "c", "d"), function(y) {sapply(1:nrow(df), function(x) {ifelse(y %in% df[x, ], TRUE, FALSE)})})))
  df
     A  B     a     b     c     d
  1 NA NA  TRUE  TRUE  TRUE FALSE
  2 NA NA FALSE  TRUE  TRUE FALSE
  3 NA NA  TRUE FALSE FALSE  TRUE
  
  identical(df, df_goal)
  # [1] TRUE
  </code></pre>
  
  <p>Are there more concise options to achieve what I want?</p>
  
  <hr>
  
  <p><strong>Edit</strong> after @Sonny's comment:</p>
  
  <p>I also thought about a <code>tidyr</code> option, but could not get there:</p>
  
  <pre><code>library(tidyr)
  df %&gt;% separate(C, c("a", "b", "c", "d"))
     A  B a b    c    d
  1 NA NA a b    c &lt;NA&gt;
  2 NA NA c b &lt;NA&gt; &lt;NA&gt;
  3 NA NA d a &lt;NA&gt; &lt;NA&gt;
  </code></pre>
  
  <p>This is still unsorted and so <code>spread</code> doesn't really work..</p>
  
  <p>What am I missing?</p>