0

I have to normalize my test set with my train set for numeric variables. I've done something like this:

testData$varnum1 <- (testData$varnum1 - mean(trainData$varnum1)) / sd(trainData$varnum1)
testData$varnum2 <- (testData$varnum2 - mean(trainData$varnum2)) / sd(trainData$varnum2)
 ...
testData$varnumN <- (testData$varnumN - mean(trainData$varnumN)) / sd(trainData$varnumN)

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

UPDATE: 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 test<- (test - mean(train))/sd(train)because test and train are dataframes. Any suggestion please?

david9ppo
  • 75
  • 9

0 Answers0