title: Creating binary variables in R from categorical and NA variables
body: <p>I have a dataset of 12901 <code>categorical</code> and <code>NA</code> observations with 34 variables. I will use the dataset for create a market segmentation study by clustering consumer demographics. </p>
  
  <p>For the <code>categorical</code> variables, I want to convert to <code>numeric</code> binary data. For example, variable <code>HouseholdIncome</code> has six categories: 50K-75k, 75k-100k, 35k-50k, 100k-125k, 150k-175k, and Other. I want <code>HouseholdIncome</code> to be broken up into six variables (0,0,0,0,0,1), (0,0,0,0,1,0), (0,0,0,1,0,0), (0,0,1,0,0,0), (0,1,0,0,0,0), and (1,0,0,0,0,0). </p>
  
  <p><strong>Question:</strong> how can I change the categorical values to binary variables, yet keep the <code>NA</code>s?</p>
  
  <p>My machine: </p>
  
  <pre><code>&gt; sessionInfo()
  R version 3.1.0 (2014-04-10)
  Platform: x86_64-apple-darwin13.1.0 (64-bit)
  </code></pre>
  
  <p>My data: </p>
  
  <pre><code>#Head of first six rows of the first six columns
  &gt; head(Store4df)
       Age Gender HouseholdIncome MaritalStatus PresenceofChildren HomeOwnerStatus
  1  55-64 Female         50k-75k        Single                 No             Own
  2   &lt;NA&gt; Female            &lt;NA&gt;          &lt;NA&gt;               &lt;NA&gt;            &lt;NA&gt;
  3   &lt;NA&gt;   Male            &lt;NA&gt;          &lt;NA&gt;               &lt;NA&gt;            &lt;NA&gt;
  4   &lt;NA&gt;   Male            &lt;NA&gt;          &lt;NA&gt;               &lt;NA&gt;            &lt;NA&gt;
  5    65+   Male        75k-100k        Single                 No             Own
  6   &lt;NA&gt; Female            &lt;NA&gt;          &lt;NA&gt;               &lt;NA&gt;            &lt;NA&gt;
  </code></pre>
  
  <p>I have read other posts about the command, but none have solutions for <code>NA</code> values. I followed a link about <a href="https://stackoverflow.com/questions/3384506/create-new-dummy-variable-columns-from-categorical-variable">Creating new dummy variable columns from categorical variables</a>. I used the second suggestion and the data in binary form, but the code did not include the <code>NA</code> values. </p>
  
  <pre><code>&gt; #Use model.matrix function to 
  &gt; binary1 &lt;- model.matrix(~ factor(Store4df$HomeMarketValue) - 1)
  &gt; #Find which rows have NA values
  &gt; which(rowSums(is.na(binary1))==ncol(binary1))
  # named integer(0)
  &gt; #Get head of model.matrix of two columns with five rows
  &gt; head(binary1, n=5)
     factor(Store4df$HomeMarketValue)100k-150k factor(Store4df$HomeMarketValue)150k-200k
  1                                          0                                         0
  2                                          0                                         0
  3                                          1                                         0
  4                                          0                                         0
  5                                          0                                         0
  </code></pre>
  
  <p><strong>EDIT:</strong> I forgot to post that I have two types of categorical variables. One with categories and <code>NA</code> values, with another having <code>TRUE</code> and <code>NA</code> values. I got an error about putting the variables with <code>TRUE</code> and <code>NA</code> values into a <code>model.matrix</code>.</p>
  
  <pre><code>&gt; model.matrix(~ -1 + . , data = Store4df)
  #Error in `contrasts&lt;-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
    contrasts can be applied only to factors with 2 or more levels
  </code></pre>
  
  <p>Here's what the variable looks like:</p>
  
  <pre><code>&gt; che &lt;- Store4df$Pets
  &gt; summary(che)
     Mode    TRUE    NA's 
  logical    3535    9628 
  </code></pre>
  
  <p>After putting one factor variable into <code>model.matrix</code>:</p>
  
  <pre><code>&gt; data &lt;- model.matrix(~  Pets, data = Store4df)
  &gt; summary(data)
  
    (Intercept)    PetsTRUE
   Min.   :1    Min.   :1  
   1st Qu.:1    1st Qu.:1  
   Median :1    Median :1  
   Mean   :1    Mean   :1  
   3rd Qu.:1    3rd Qu.:1  
   Max.   :1    Max.   :1  
  </code></pre>
  
  <p>How can I get the TRUE value replaced in columns 10 and 12:34? </p>