Re: [R] update index in
Kim Milferstedt 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. > > > Thanks already, > > Kim > 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 __ 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.
Re: [R] update index in "for" statement during calculation
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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] > 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]] __ 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.
[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: [EMAIL PROTECTED] 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.