title: Change level of multiple factor variables
body: <p>everyone -</p>
  
  <p>I want to preface this by saying that I already looked at this link to try to solve my problem:</p>
  
  <p><a href="https://stackoverflow.com/questions/15484236/applying-the-same-factor-levels-to-multiple-variables-in-an-r-data-frame">Applying the same factor levels to multiple variables in an R data frame</a></p>
  
  <p>The difference is that in that problem, the OP wanted to change the levels of factors that all had the same levels. In my instance, I'm looking to change just the first level, which is set to ' ', to something like 'Unknown' and leave the rest of the levels alone. I know I could do this in a "non-R" way with something like this:</p>
  
  <pre><code>for (i in 64:88) {
    var.name &lt;- colnames(df[i])
    levels(eval(parse(text=paste('df$', var.name, sep=''))))[levels(eval(parse(text=paste('df$', var.name, sep='')))) == ' '] &lt;- 'Unknown'
  }
  </code></pre>
  
  <p>But that's an inefficient way to do it. Trying to use the method proposed in the question linked above gave me this code:</p>
  
  <pre><code>df[64:88] &lt;- lapply(df[64:88], factor, levels=c('Unknown', ??))
  </code></pre>
  
  <p>I don't know what to put in place of the question marks. I tried using just "levels[-1]" but it's obvious why that didn't work. I also tried "levels(df[64:88])[-1]" but again no good. So I tried to revamp the code with the following:</p>
  
  <pre><code>df[64:88] &lt;- lapply(df[64:88], function(x) levels(x)[levels(x) == ' '] &lt;- 'Unknown')
  </code></pre>
  
  <p>but I get NULL whenever I call levels$transaction_type1 (where transaction_type1 is the column name of df[64]).</p>
  
  <p>What am I missing here?</p>
  
  <p>Thanks in advance for your help!</p>
  
  <p>Per a couple of requests, here is an example of my data:</p>
  
  <pre><code>df$transaction_type1[1:100]
    [1]                                                                                                                                                
   [13] HOME RENEW                                                                                                                                     
   [25]                                                                                                                                                
   [37]                                                                                                                                                
   [49]                                                                                                                                                
   [61] AUTO MANAGE                                                                                     AUTO RENEW                                     
   [73]             AUTO MANAGE                                                                                     AUTO RENEW                         
   [85]                                                                                                                                                
   [97]                                                
  Levels:   AUTO CLAIM AUTO MANAGE AUTO PURCHASE AUTO RENEW HOME CLAIM HOME RENEW
  </code></pre>
  
  <p>As you can see, there is a lot of values equal to ' ' and all 25 variables look just like this, but with different levels. My data consists of 222 variables and 24,850 rows, so I don't know what the standard is on SO for giving example data. Also, this snippet of code might help as well:</p>
  
  <pre><code>&gt; levels(df$transaction_type1)
  #[1] " "             "AUTO CLAIM"    "AUTO MANAGE"   "AUTO PURCHASE" "AUTO RENEW"    "HOME CLAIM"    "HOME RENEW"
  
  &gt; levels(df$transaction_type1)[levels(df$transaction_type1) == ' '] &lt;- 'Unknown'
  &gt; levels(df$transaction_type1)
  #[1] "Unknown"       "AUTO CLAIM"    "AUTO MANAGE"   "AUTO PURCHASE" "AUTO RENEW"    "HOME CLAIM"    "HOME RENEW"   
  </code></pre>
  
  <p>If more information is needed, please let me know so I can provide it and also learn the SO standards of asking for help. Thanks!</p>