title: Replace all occurrences of a string in a data frame
body: <p>I'm working on a data frame that has non-detects which are coded with '&lt;'.  Sometimes there is a space after the '&lt;' and sometimes not e.g. '&lt;2' or '&lt; 2'.  I'd like to remove every occurrence of the space.</p>
  
  <p>Example:</p>
  
  <pre><code>data &lt;- data.frame(name = rep(letters[1:3], each = 3), var1 = rep('&lt; 2', 9), var2 = rep('&lt;3', 9))
  
    name var1 var2 
  1    a  &lt; 2   &lt;3
  2    b  &lt; 2   &lt;3
  3    c  &lt; 2   &lt;3
  </code></pre>
  
  <p>This is where I've got to:</p>
  
  <p>I can extract all the values and make the new strings but I can't put them back in the data frame.</p>
  
  <pre><code>index &lt;- str_detect(unlist(data), '&lt;')
  index &lt;- matrix(index, nrow = 3)
  
  data[index] 
  #[1] "&lt; 2" "&lt; 2" "&lt; 2" "&lt;3"  "&lt;3"  "&lt;3" 
  
  replacements &lt;- str_replace_all(data[index], "&lt;[ ]+","&lt;") 
  replacements
  #[1] "&lt;2" "&lt;2" "&lt;2" "&lt;3" "&lt;3" "&lt;3"
  
  data[index] &lt;- replacements
  
  #Error in `[&lt;-.data.frame`(`*tmp*`, index, value = c("&lt;2", "&lt;2", "&lt;2",  : 
  #  unsupported matrix index in replacement
  </code></pre>