How to simplify this normalization code?
<p>I have to normalize my test set with my train set for numeric variables. I've done something like this:</p>

<pre><code>testData$varnum1 &lt;- (testData$varnum1 - mean(trainData$varnum1)) / sd(trainData$varnum1)
testData$varnum2 &lt;- (testData$varnum2 - mean(trainData$varnum2)) / sd(trainData$varnum2)
 ...
testData$varnumN &lt;- (testData$varnumN - mean(trainData$varnumN)) / sd(trainData$varnumN)
</code></pre>

<p>How I can simplify this code to do it more generally using some "apply" function or similar?</p>

<p><strong>UPDATE</strong>: I think that the suggested post don´t solve my question. My code works but is not general because I have one code line by each numeric var. I only need to know how do it more general. I can't use scale function because my normalization uses train and test sets. And I can´t do <code>test&lt;- (test - mean(train))/sd(train)</code>because test and train are dataframes. Any suggestion please?</p>