title: Replace NA with mode from categorical dataset R
body: <p>I have a dataset with <code>categorical</code> and <code>NA</code> observations of 10 variables. I want to replace the <code>NA</code> values of each column with the mode. I did a histogram of each variable for identifying the density for each observation and got the mode. I know what values to replace the <code>NA</code>s in each column with.</p>
  
  <p>I saw there was a related post, but I already know what values to replace. Here's the link: <a href="https://stackoverflow.com/questions/7731192/replace-mean-or-mode-for-missing-values-in-r">Replace mean or mode for missing values in R</a> </p>
  
  <p>Here's to reproduce the dataset: </p>
  
  <pre><code>&gt; #Create data with missing values
  &gt; set.seed(1)
  &gt; dat &lt;- data.frame(x=sample(letters[1:3],20,TRUE), y=rnorm(20), 
                                                    stringsAsFactors=FALSE)
  &gt; dat[c(5,10,15),1] &lt;- NA
  </code></pre>
  
  <p>Here's an example:</p>
  
  <pre><code>&gt; #The head of the first five observations
  &gt; head(SmallStoredf, n=5)
  
      Age Gender HouseholdIncome MaritalStatus PresenceofChildren HomeOwnerStatus HomeMarketValue
  1  &lt;NA&gt;   Male            &lt;NA&gt;          &lt;NA&gt;               &lt;NA&gt;            &lt;NA&gt;            &lt;NA&gt;
  2 45-54 Female            &lt;NA&gt;          &lt;NA&gt;               &lt;NA&gt;            &lt;NA&gt;            &lt;NA&gt;
  5 45-54 Female        75k-100k       Married                Yes             Own       150k-200k
  6 25-34   Male        75k-100k       Married                 No             Own       300k-350k
  7 35-44 Female       125k-150k       Married                Yes             Own       250k-300k
    Occupation             Education LengthofResidence
  1       &lt;NA&gt;                  &lt;NA&gt;              &lt;NA&gt;
  2       &lt;NA&gt;                  &lt;NA&gt;              &lt;NA&gt;
  5       &lt;NA&gt; Completed High School           9 Years
  6       &lt;NA&gt; Completed High School       11-15 years
  7       &lt;NA&gt; Completed High School           2 Years  
  </code></pre>
  
  <p>In this example, I want <code>NA</code>s in <code>HomeOwnerStatus</code> replaced with <code>Own</code>, <code>HomeMarketValue</code> with <code>350K-500K</code>, and <code>Occupation</code> with <code>Professional</code>.</p>
  
  <p><strong>EDIT:</strong> I tried inputting the values in, but got an error about three of the columns. </p>
  
  <pre><code>&gt; replacementVals &lt;- c(Age = "45-54", Gender = "Male", HouseholdIncome = "50K-75K", 
  +                      MaritalStatus = "Single", PresenceofChildren = "No",
  +                      HomeOwnerStatus = "Own", HomeMarketValue = "350K-500K",
  +                      Occupation = "Professional", Education = "Completed High School",
  +                      LengthofResidence = "11-15yrs")
  &gt; indx1 &lt;- replacementVals[col(df2)][is.na(df2[,names(replacementVals)])]
  &gt; df2[is.na(df2[,names(replacementVals)])]  &lt;- indx1
  #Warning messages:
  #1: In `[&lt;-.factor`(`*tmp*`, thisvar, value = c("50K-75K", "50K-75K",  :
    invalid factor level, NA generated
  #2: In `[&lt;-.factor`(`*tmp*`, thisvar, value = c("350K-500K", "350K-500K",  :
    invalid factor level, NA generated
  #3: In `[&lt;-.factor`(`*tmp*`, thisvar, value = c("11-15yrs", "11-15yrs",  :
    invalid factor level, NA generated
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>&gt; head(SmallStoredf)
  
      Age Gender HouseholdIncome MaritalStatus PresenceofChildren HomeOwnerStatus HomeMarketValue
  1 45-54   Male            &lt;NA&gt;        Single                 No             Own            &lt;NA&gt;
  2 45-54 Female            &lt;NA&gt;        Single                 No             Own            &lt;NA&gt;
  5 45-54 Female        75k-100k       Married                Yes             Own       150k-200k
  6 25-34   Male        75k-100k       Married                 No             Own       300k-350k
  7 35-44 Female       125k-150k       Married                Yes             Own       250k-300k
  8 55-64   Male        75k-100k       Married                 No             Own       150k-200k
      Occupation             Education LengthofResidence
  1 Professional Completed High School              &lt;NA&gt;
  2 Professional Completed High School              &lt;NA&gt;
  5 Professional Completed High School           9 Years
  6 Professional Completed High School       11-15 years
  7 Professional Completed High School           2 Years
  8 Professional Completed High School       16-19 years
  </code></pre>
  
  <p>Only <code>NA</code> values in some columns were replaced. </p>