Convert data frame of values into binary data frame where each unique value is a column
<p>I'm reading in data from a csv file where each row contains some number of individual strings:</p>

<p>e.g.</p>

<pre><code>data.csv -&gt;
    x,f,t,h,b,g
    d,g,h
    g,h,a,s,d
    f
    q,w,e,r,t,y,u,i,o

data &lt;- read.csv("data.csv", header = FALSE)
</code></pre>

<p>I want to transform this input into a data frame where the columns are the set of unique strings present in the input. In this case, the columns would be the set of strings <code>{x,f,t,h,b,g,d,a,s,q,w,e,r,y,u,i,o}</code>. Additionally, the new data frame should contain a row for each row in the input data frame such that a column will have the value <code>1</code> if the column's name was present in that row in the input data frame, or <code>0</code> if the column's name was not present in that input row.</p>

<p>In this example, the desired output would be the following:</p>

<pre><code>    x f t h b g d a s q w e r y u i o
   ----------------------------------
1 | 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
2 | 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0
3 | 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0
4 | 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 | 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1
</code></pre>

<p>The code below is what I currently have. However, the output <code>df</code> ends up being a data frame with what appear to be the correct columns, but 0 rows.</p>

<p>I'm very inexperienced at R, and this was my attempt at putting together something that works. It seems to work as expected up until the call to <code>apply()</code>, which unexpectedly doesn't add anything to <code>df</code>.</p>

<pre><code>data &lt;- read.csv("data.csv", header = FALSE)

columnNames = c()
for (row in data) {
  for (eventName in row) {
    if (!(eventName %in% columnNames)) {
      columnNames = c(columnNames, eventName)
    }
  }
}
columnNames = t(columnNames)

df = data.frame(columnNames)
colnames(df) = columnNames
df = df[-1,]

apply(data, 1, function(row, df) {
  dat = data.frame(columnNames)
  colnames(dat) = columnNames
  dat = dat[-1,]

  for (eventName in row) {
    if (eventName != "") {
      dat[1,eventName] = 1
    }
  }

  df = rbind(df, dat)
}, df)
</code></pre>

<p>After the script finishes it tells me there were many warnings of the following two forms:</p>

<pre><code>9: In `[&lt;-.factor`(`*tmp*`, iseq, value = 1) : invalid factor level, NA generated
10: In `[&lt;-.factor`(`*tmp*`, iseq, value = 1) :
  invalid factor level, NA generated
</code></pre>