R make new columns from uniqe values in column dynamically : <p>I want to create new columns based on unique values in one column dynamically. Original:</p>

<pre><code>id, category
1, a
2, b
3, c
4, b
</code></pre>

<p>New:</p>

<pre><code>id, category, a, b, c
1, a, 1, 0, 0
2, b, 0, 1, 0
3, c, 0, 0, 1
4, b, 0, 1, 0
</code></pre>

<p>For now I do:</p>

<pre><code>data$categoryA = ifelse(data$category=="a", 1, 0)
data$categoryB = ifelse(data$category=="b", 1, 0)
...
</code></pre>

<p>But I want to do this dynamically something like this:</p>

<pre><code>for(CATEGORY in unique(data$category) {
  data$CATEGORY  = ifelse(data$CATEGORY =="a", 1, 0)
}
</code></pre>
