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))
}