title: Merge two factor columns in R
body: <p>Hi I am having trouble with something in R. I'm trying to merge (combine?) two (factor) columns in a data frame. For each row, there is a value in only one of the columns and I want to combine them so that all the rows have a value. As a simplified example, suppose I've run the following code: <code>df &lt;- data.frame(x=c("a", "b", " ", " "), y=c(" ", " ", "q", " "), z=c(" ", " ", " ", "p"))</code>, I get the following data frame</p>
  
  <pre><code>    x   y
  1   a   
  2   b        
  3       q
  </code></pre>
  
  <p>After the x and y columns are merged, The result would be</p>
  
  <pre><code>  x y merged
  1 a        a
  2 b        b
  3   q      q
  </code></pre>
  
  <p>I have tried using <code>df$merged = ifelse(df$x == " ", df$y, df$x)</code>, but it gives me these numbers. Any idea what they mean?</p>
  
  <pre><code>  x y merged
  1 a        2
  2 b        3
  3   q      2
  </code></pre>
  
  <p>All the other helpful information I have come across works well with numbers, but not characters. Am I on the right track with what I have tried so far?</p>
  
  <p>It seems like such a simple problem but I have not been able to find a solution. Any help would be appreciated. </p>
  
  <p>Thanks all.</p>