[R] update index in "for" statement during calculation
Kim Milferstedt
milferst at uiuc.edu
Wed Oct 25 19:31:08 CEST 2006
Hello,
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,
Kim
### 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
USA
phone: (001) 217 333-9663
fax: (001) 217 333-6968
email: milferst at uiuc.edu
http://cee.uiuc.edu/research/morgenroth
More information about the R-help
mailing list