title: How can I add two columns to a data.table with random sampling of a binary possibility?
body: <p>I have a data table of children where I know one ear was affected but I do not know which one. I want to randomly allocate the issue to either the right ear or the left ear. I do not care which but if the one ear is designated &quot;in&quot; then the other ear must be designated &quot;out&quot;.</p>
  <p>That is easy I thought for just one child</p>
  <pre><code>sample(c(&quot;in&quot;, &quot;out&quot;), size = 2)
  </code></pre>
  <p>Then I thought that I could simply assign the results to the right and left column.</p>
  <pre><code>children &lt;- data.table(child = c(&quot;Gertrude&quot;, &quot;Penelope&quot;, &quot;Joshua&quot;, &quot;Sebastian&quot;, &quot;Jane&quot;, &quot;Ravi&quot;))
  children[, c(&quot;right&quot;, &quot;left&quot;) := sample(c(&quot;in&quot;, &quot;out&quot;), size = 2)]
  </code></pre>
  <p>I have messed around a lot with the code and I keep getting the following message. It is not helping me.</p>
  <blockquote>
  <p>Error in <code>[.data.table</code>(children, , <code>:=</code>(c(&quot;right&quot;, &quot;left&quot;),
  sample(c(&quot;in&quot;,  :    Supplied 2 items to be assigned to 6 items of
  column 'right'. If you wish to 'recycle' the RHS please use rep() to
  make this intent clear to readers of your code.</p>
  </blockquote>
  <p>I also tried adding <code>by=child</code>.</p>
  <p>Can you please help me?</p>