Kim Milferstedt
2006-Oct-25 17:31 UTC
[R] update index in "for" statement during calculation
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
try using a 'while': i <- 1 while (i < 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 ,]) } i <- i+1 } On 10/25/06, Kim Milferstedt <milferst@uiuc.edu> wrote:> > 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@uiuc.edu > http://cee.uiuc.edu/research/morgenroth > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Kim Milferstedt <milferst <at> uiuc.edu> writes:> > 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. ><SNIP>> > Thanks already, > > Kim ><SNIP> I believe this will also do what you want:> days<-c(1:10)[-5:-7] > xx<-rnorm(7) > data<-data.frame(xx,days) > new.data<-merge(data,data.frame(days=1:10),all.y=TRUE)It usually is not a good idea to use zeroes as placeholders for missing values. Mark Lyman
Seemingly Similar Threads
- R for copying and pasting selected image files?
- nested if/else very slow, more efficient ways?
- removing a specific number of digist from a character string
- barplot with different color combination for each bar
- which points within an ellipsoid? Sorting data in 3d