Creating a new Dataframe from loop on old dataframe in R -


i have dataframe time series of prices(pt) looks follows:

head(mydata)                b     c 2005-07-29 66.82 41.80 372.0 2005-08-01 68.94 42.70 373.0 2005-08-02 70.22 43.75 379.2 2005-08-03 70.79 43.75 385.6 2005-08-04 70.42 44.10 390.0 2005-08-05 69.84 43.60 386.0  

consideri ng lookback window of 3 days (w=3), trying create new dataframe time series of following:

x(ti) = 1/w (pt/pt + pt-1/pt + pt-2/pt)

i have used following code:

nr<-nrow(mydata)  nc<-ncol(mydata)  x<-data.frame()  (j in 1:nc){     for(i in 1:nr-w+1){     x[i,j]<-rowsums(mydata[i:(w+i-1),j])                 } } 

however, following error message:

error in rval[i, j, drop = drop., ...] :  0's may mixed negative subscripts 

this seems suggest wrong loop? or syntax? appreciated.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -