0

I am currently engaged in predictive time series analytics. Recently began working with R. Could you please help me optimize the function that takes the previous line(s), specified by the step parameter, and puts them in the same row as the current observation.

The problem is that it takes a very long time. I use the functions cbind() and rbind() and I suspect that the issue lies with them. It could also be because I am using data.frame. Any feedback will be greatly appreciated! My code:

lastValAsFeatures=function(obj, step=2)
{
 res=1:(step*ncol(obj))
 pb <- txtProgressBar(min = 0, max = nrow(obj), style = 3)
 n=ncol(obj)
 for(i in step: nrow(obj))
{
  ftrs=0
  for(j in (i-step+1):(i-1))
 {
    ftrs=cbind(ftrs, obj[j,])
 }
ftrs=ftrs[,-1]
res=rbind(res,cbind(obj[i,], ftrs))
setTxtProgressBar(pb, i)
}
res=res[-1,]
close(pb)
return(as.data.frame(res))
}
Dinesh.hmn
  • 713
  • 7
  • 21
  • pre-allocate memory. Growing objects is a very incredibly big no-no in R. Next to that, please be more specific about what the code does, expected input, expected output. Best include a reproducible example. See also : http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to create a minimal reproducible example. – Joris Meys Mar 07 '17 at 14:59
  • The discussion about speeding up code for this question should help you: http://stackoverflow.com/questions/2908822/speed-up-the-loop-operation-in-r – p0bs Mar 07 '17 at 16:34

0 Answers0