R, mutate and "Unsupported type NILSXP for column"
<p>Here's a snapshot of my data:  </p>

<pre><code>  structure(list(CPUBID = c(1000001L, 1000002L, 1000003L, 10001L, 
1000201L, 1000203L, 10003L, 1000801L, 1000802L, 1000803L, 1001L, 
1001101L, 1001102L, 1001601L, 1002401L, 1002402L, 1002403L, 1002601L, 
1002602L, 1002604L), MPUBID = c(10000L, 10000L, 10000L, 100L, 
10002L, 10002L, 100L, 10008L, 10008L, 10008L, 10L, 10011L, 10011L, 
10016L, 10024L, 10024L, 10024L, 10026L, 10026L, 10026L), CYRB = c(1982L, 
1984L, 1988L, 1985L, 1986L, 1992L, 1993L, 1984L, 1986L, 1988L, 
1983L, 1987L, 1992L, 1977L, 1981L, 1984L, 1998L, 1980L, 1981L, 
1984L), twinfam = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SAMESEX = c(1L, 1L, 1L, 
1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L), top25 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 
0, 0, 0, 0), top5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), quantity = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1)), .Names = c("CPUBID", "MPUBID", 
"CYRB", "twinfam", "SAMESEX", "top25", "top5", "quantity"), row.names = c(NA, 
20L), class = "data.frame")
</code></pre>

<p>I'm trying to use the twinfam(twin in family) and SAMESEX(first two born children same sex) binary variables to create a fourth variable that takes on 4 possible values:</p>

<ul>
<li><p>1 if SAMESEX == 0 &amp; twinfam == 0</p></li>
<li><p>2 if SAMESEX == 1 &amp; twinfam == 0</p></li>
<li><p>3 if SAMESEX == 0 &amp; twinfam ==1</p></li>
<li><p>4 if SAMESEX == 1 &amp; twinfam ==1</p></li>
</ul>

<p>After playing around a bit I tried using:</p>

<pre><code>df &lt;- df %&gt;% mutate(both = for (i in 1:nrow(PIATmathreg6)) {
                              if(twinfam[i] == 0 &amp; SAMESEX[i] == 0) both = 1
                              else if(twinfam[i] == 0 &amp; SAMESEX[i] == 1) both = 2
                              else if(twinfam[i] == 1 &amp; SAMESEX[i] == 0) both = 3
                              else both = 4})
</code></pre>

<p>but I get the error:</p>

<pre><code>Error: Unsupported type NILSXP for column "both"
</code></pre>

<p>and can't seem to resolve this error. Any advice on why I get this error, and how it might be resolved, would be appreciated!</p>