[R] update index in "for" statement during calculation

From: Kim Milferstedt <milferst_at_uiuc.edu>
Date: Wed 25 Oct 2006 - 17:31:08 GMT


I have a time series of data as a data.frame. Occasionally there is one or more days missing (e.g. data available for days 2, 3, 4, 8, 9, 10 --> missing days between 4 and 8). The experimental time information can be found in the 2nd column of "data". I would like to have a continuous time line with one time point per day. Therefore I try to insert lines for the missing days that contain zeros for the data categories just to fill the columns.

In most cases the code below works fine but there is one problem: Since apparently the "for" statement does not get updated with the exanding data.frame "data" (as lines get inserted), the "for" statement ends somewhere before the end of the now longer modified version of "data". Therefore potential missing data points afterwards are not inserted any longer.

Does anybody know how I can update the information on "i" in the "for" statement along the way of the calculation? Or does anybody know a way around my problem?

Thanks already,


### Code ###

for (i in 1:(nrow(data)-1))

     diff.time    <- round(data[i+1,2], 0) - round(data[i,2], 0)-1
     old.row              <-  nrow(data)
     if (diff.time > 0)
                 fill                <-  c(data[i,1], 
(round(data[i,2], 0)+1), rep(0,classnumber))
                 data        <-  rbind(data[1:i,], fill, data[(i+1):old.row,])


Kim Milferstedt
University of Illinois at Urbana-Champaign Department of Civil and Environmental Engineering 4125 Newmark Civil Engineering Laboratory 205 North Mathews Avenue MC-250
Urbana, IL 61801
phone: (001) 217 333-9663
fax: (001) 217 333-6968
email: milferst@uiuc.edu


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 26 04:30:56 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 25 Oct 2006 - 19:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.