title: Applying multiple function via sapply
body: <p>I'm trying to replicate solution on applying multiple functions in <code>sapply</code> posted on <a href="http://www.r-bloggers.com/applying-multiple-functions-to-data-frame/" rel="nofollow noreferrer">R-Bloggers</a> but I can't get it to work in the desired manner. I'm working with a simple data set, similar to the one generated below:</p>
  
  <pre><code>require(datasets)
  crs_mat &lt;- cor(mtcars)
  
  # Triangle function
  get_upper_tri &lt;- function(cormat){
    cormat[lower.tri(cormat)] &lt;- NA
    return(cormat)
  }
  
  require(reshape2)
  crs_mat &lt;- melt(get_upper_tri(crs_mat))
  </code></pre>
  
  <p>I would like to replace some text values across columns <em>Var1</em> and <em>Var2</em>. The <strong>erroneous</strong> syntax below illustrates what I am trying to achieve:</p>
  
  <pre><code>crs_mat[,1:2] &lt;- sapply(crs_mat[,1:2], function(x) {
   # Replace first phrase
   gsub("mpg","MPG",x), 
   # Replace second phrase
    gsub("gear", "GeArr",x)
   # Ideally, perform other changes
  })
  </code></pre>
  
  <p>Naturally, the code is not syntactically correct and fails. To summarise, I would like to do the following:</p>
  
  <ol>
  <li>Go through all the values in first two columns <em>(Var1 and Var2)</em> and perform simple replacements via <code>gsub</code>.</li>
  <li>Ideally, I would like to avoid defining a separate function, as discussed in the <a href="http://www.r-bloggers.com/applying-multiple-functions-to-data-frame/" rel="nofollow noreferrer">linked</a> post and keep everything <em>within</em> the <code>sapply</code> syntax</li>
  <li>I don't want a nested loop</li>
  </ol>
  
  <hr>
  
  <p>I had a look at the broadly similar subject discussed <a href="https://stackoverflow.com/a/15215131/1655567">here</a> and <a href="https://stackoverflow.com/a/7406550/1655567">here</a> but, if possible, I would like to avoid making use of <code>plyr</code>. I'm also interested in replacing the column values not in creating new columns and I would like to avoid specifying any column names. While working with my existing data frame it is more convenient for me to use column numbers.</p>
  
  <h1>Edit</h1>
  
  <p>Following very useful comments, what I'm trying to achieve can be summarised in the solution below:</p>
  
  <pre><code>fun.clean.columns &lt;- function(x, str_width = 15) {
    # Make character
    x &lt;- as.character(x)
    # Replace various phrases
    x &lt;- gsub("perc85","something else", x)
    x &lt;- gsub("again", x)
    x &lt;- gsub("more","even more", x)
    x &lt;- gsub("abc","ohmg", x)
    # Clean spaces
    x &lt;- trimws(x)
    # Wrap strings
    x &lt;- str_wrap(x, width = str_width)
    # Return object
    return(x)
  }
  mean_data[,1:2] &lt;- sapply(mean_data[,1:2], fun.clean.columns)
  </code></pre>
  
  <p>I don't need this function in my <code>global.env</code> so I can run <code>rm</code> after this but even nicer solution would involve <em>squeezing</em> this within the <code>apply</code> syntax.</p>