title: Attempting to replace character value in dataframe with numeric value , Error " invalid factor level, NA generated"
body: <p>I’m trying to do some that preprocessing, and want to convert the <code>classe</code> factors values <code>{A,B,C,D,E}</code> to <code>{1,2,3,4,5}</code>.</p>
  
  <p>The <code>classe</code> column is of type <code>factor</code>, I have provided all steps, see below:</p>
  
  <pre><code>    #get the data
      training &lt;- read.table("http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv",header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
      training_df &lt;- data.frame(training,stringsAsFactors=FALSE)
  
      #split to training &amp; test sets
      inTrain &lt;- createDataPartition(y=training$classe, p=0.75, list=FALSE)
      training_data &lt;- training[inTrain,]
      testing_data &lt;- training[-inTrain,]
  
      #subset based on columns of interest, based on previous studies
      training_data_subset &lt;- subset(training_data, select=c("avg_roll_belt","var_roll_belt","var_total_accel_belt","amplitude_roll_belt","max_roll_belt","var_roll_belt",
      "var_accel_arm","magnet_arm_x","magnet_arm_y","magnet_arm_z","accel_dumbbell_y","accel_dumbbell_z","magnet_dumbbell_x","gyros_dumbbell_x",
      "gyros_dumbbell_y","gyros_dumbbell_z","pitch_forearm","gyros_forearm_x","gyros_forearm_y","classe"))
  
      #see which columns are factors, the training_data_subset#classe feature is a factor
      sapply(training_data_subset, class)
  
  #sapply output
  
   avg_roll_belt        var_roll_belt var_total_accel_belt  amplitude_roll_belt        max_roll_belt 
             "numeric"            "numeric"            "numeric"            "numeric"            "numeric" 
       var_roll_belt.1        var_accel_arm         magnet_arm_x         magnet_arm_y         magnet_arm_z 
             "numeric"            "numeric"            "integer"            "integer"            "integer" 
      accel_dumbbell_y     accel_dumbbell_z    magnet_dumbbell_x     gyros_dumbbell_x     gyros_dumbbell_y 
             "integer"            "integer"            "integer"            "numeric"            "numeric" 
      gyros_dumbbell_z        pitch_forearm      gyros_forearm_x      gyros_forearm_y               classe 
             "numeric"            "numeric"            "numeric"            "numeric"             "factor" 
  </code></pre>
  
  <p>I created a function that attempts to replace A=1,B=2,C=3,D=4,E=5, see below:</p>
  
  <pre><code>factorsToNumeric &lt;- function(data)
  {
      data_numeric &lt;- data
      data_numeric$classe &lt;-as.numeric(factor(toupper(as.character(data_numeric$classe))))
      #loop through the data frame based on replace values
      for(i in 1:nrow(data_numeric)) 
      {
  
      if ((data_numeric[i,]$classe == "A") || (data_numeric[i,]$classe  == "a")) 
      {data_numeric[i,]$classe &lt;- "1"}
      else if ((data_numeric[i,]$classe  == "B") || (data_numeric[i,]$classe  == "b"))
      {data_numeric[i,]$classe &lt;- "2"}
      else if ((data_numeric[i,]$classe  == "C") || (data_numeric[i,]$classe  == "c"))
      {data_numeric[i,]$classe &lt;- "3"}
      else if ((data_numeric[i,]$classe  == "D") || (data_numeric[i,]$classe  == "d"))
      {data_numeric[i,]$classe &lt;- "4"}
      else if ((data_numeric[i,]$classe  == "E") || (data_numeric[i,]$classe  == "e"))
      {data_numeric[i,]$classe &lt;- "5"}
      else 
      {
      #do nothing 
      }
  
      }
  
      return (data_numeric)
  }
  </code></pre>
  
  <p>However, I get this error:</p>
  
  <pre><code>training_data_subset_numeric &lt;- factorsToNumeric(training_data_subset)
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Warning messages:
  1: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  2: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  3: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  4: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  5: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  6: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  7: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  8: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  9: In `[&lt;-.factor`(`*tmp*`, iseq, value = "1") :
    invalid factor level, NA generated
  </code></pre>
  
  <p>Further inspection shows the column "classe"'s class is converted to "numeric":</p>
  
  <pre><code> sapply(training_data_subset_numeric, class)
  
   avg_roll_belt        var_roll_belt var_total_accel_belt  amplitude_roll_belt        max_roll_belt 
         "numeric"            "numeric"            "numeric"            "numeric"            "numeric" 
   var_roll_belt.1        var_accel_arm         magnet_arm_x         magnet_arm_y         magnet_arm_z 
         "numeric"            "numeric"            "integer"            "integer"            "integer" 
  accel_dumbbell_y     accel_dumbbell_z    magnet_dumbbell_x     gyros_dumbbell_x     gyros_dumbbell_y 
         "integer"            "integer"            "integer"            "numeric"            "numeric" 
  gyros_dumbbell_z        pitch_forearm      gyros_forearm_x      gyros_forearm_y               classe 
         "numeric"            "numeric"            "numeric"            "numeric"            "numeric"
  </code></pre>
  
  <p>However, the head function confirms the error above &amp; all the values A,B,C,D,E have been replaced with <code>NA</code> incorrectly.</p>