title: Using ifelse() with R and text removal: how to handle NA values?
body: <p>I have a column of data that has mixed units. I'm trying to use <code>ifelse()</code> to standardize the minute values to hours, which is the other unit.</p>
  
  <p>Starting with data like:</p>
  
  <pre><code>test_df &lt;- data.frame(
    median_playtime = c("2.5 hours", "9 minutes", "20 hours")
  )
  </code></pre>
  
  <p>I'm trying this:</p>
  
  <pre><code>  test_df$median_playtime_hours &lt;- ifelse(
  
    #if the data has hours in it, then...
    test = length(grep("hours", as.character(test_df$median_playtime) ,value=FALSE)) == 1
  
    #text removal if it contains hours
    ,as.numeric(gsub(pattern = " hours", replacement = "", x = as.character(test_df$median_playtime)))
  
    #otherwise, remove minutes text and divide by 60
    ,as.numeric(gsub(pattern = " minutes", replacement = "", x = test_df$median_playtime)) / 60
  )
  </code></pre>
  
  <p>Each conditional line works ok but produces NAs for the mismatch cases, so the end result is NAs across the board. Is there a way to either ignore the NAs or merge the two conditions so the NAs aren't the only value returned?</p>