title: How can I write a function that is iterable?
body: <p>I need to modify a function (below) that will apply row-wise with <code>dplyr::mutate</code> to remove any '_' characters and capitalise the first letter of each word.</p>
  
  My function
  
  <pre><code>simple_cap &lt;- function(x) {
    s &lt;- strsplit(x, "_")[[1]]
    paste(toupper(substring(s, 1,1)), substring(s, 2),
          sep="", collapse=" ")
  }
  </code></pre>
  
  My data
  
  <pre><code>df &lt;- read.table(text = c('
  location             obs
  
  1 australia         12454.
  2 new_south_wales    3931.
  3 victoria           3244.
  4 queensland         2477.
  5 south_australia     834.
  6 western_australia  1335.
  7 tasmania            246.'), stringsAsFactors = F)
  </code></pre>
  
  The <code>dplyr::mutate</code> call:
  
  <pre><code>df %&gt;% mutate(
    location = simple_cap(location)
  )
  </code></pre>
  
  The output
  
  <pre><code>location   obs
  1 Australia 12454
  2 Australia  3931
  3 Australia  3244
  4 Australia  2477
  5 Australia   834
  6 Australia  1335
  7 Australia   246
  </code></pre>
  
  <p>How can I change my function so that it can be used to iterate over the values in <code>df$location</code> rather than replacing them all with the output from the first element?</p>