0

I am trying to normalize the data frame before prediction but I get this error :

Error in seq_len(nrows)[i] :
only 0's may be mixed with negative subscripts
Called from: top level

Here is my code :

 library('caret')
 load(file = "some dataset path here") 
 DummyDataSet =  data
 attach(DummyDataSet)
 foldCount = 10
 classifyLabels = DummyDataSet$ClassLabel
 folds = createFolds(classifyLabels,k=foldCount)
 for (foldIndex in 1:foldCount){
    cat("----- Start Fold -----\n")

    #holding out samples of one fold in each iterration
    testFold    = DummyDataSet[folds[[foldIndex]],]     
    testLabels  = classifyLabels[folds[[foldIndex]]]
    trainFolds  = DummyDataSet[-folds[[foldIndex]],]
    trainLabels = classifyLabels[-folds[[foldIndex]]]

#Zero mean unit variance normalization to ONLY numerical data
for (k in 1:ncol(trainFolds)){
   if (!is.integer(trainFolds[,k])){
      params = meanStdCalculator(trainFolds[,k])
      trainFolds[,k] = sapply(trainFolds[,k], function(x) (x - params[1])/params[2])
      testFold[,k]  = sapply(testFold[,k],  function(x) (x - params[1])/params[2])
   }
}
  meanStdCalculator = function(data){  
    Avg  = mean(data)
    stdDeviation  = sqrt(var(data))
    return(c(Avg,stdDeviation))
  }
  cat("----- Start Fold -----\n")
}

where trainFolds is a fold creating by caret package and its type is data.frame. I have already read these links :

R Debugging

Subset

Negative Subscripts

but I couldn't find out what is wrong with the indexes? anybody can help me?

Community
  • 1
  • 1
nooshinha
  • 249
  • 5
  • 15
  • 3
    Please consider posting your problem as a fully working example and what the desired output would look like. You can find some tips on how to do that [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Roman Luštrik Apr 28 '15 at 08:25
  • the reason I didn't create a verifiable example is that the code was too much. I couldn't put all the script here. I will try to remove unnecessary parts and edit my post again. – nooshinha Apr 28 '15 at 09:40
  • Try simulating the data and post a minimal script that still demonstrates your problem. – Roman Luštrik Apr 28 '15 at 09:41
  • please have a look on the new script, I have simulated it and it works fine for me. – nooshinha Apr 28 '15 at 09:45
  • by the way `dput()` says :`Error during wrapup: argument "x" is missing, with no default` – nooshinha Apr 28 '15 at 09:53
  • 2
    You need to specify which object you want to "dump". Your script probably won't work for me because I don't have a "some dataset path here" dataset. Please simulate data or provide a working dump of your actual data (if there are no restrictions on showing it). – Roman Luštrik Apr 28 '15 at 11:19

0 Answers0