From: Gabor Grothendieck <ggrothendieck_at_gmail.com>

Date: Sat 19 Aug 2006 - 00:26:15 EST

Date: Sat 19 Aug 2006 - 00:26:15 EST

I did run it so I am not sure how the error crept in. Anyways, I have fixed it and a corrected version is below.

--- Here are two solutions. In both we break up DF into rows which start with 1. In #1 we create a new data frame with the required sequence for A and zeros for B and then we fill it in. In #2 we convert each set of rows to a zoo object z where column A is the times and B is the data. We convert that zoo object to a ts object (which has the effect of filling in the missing times) and then create a zoo object with no data from that merging it with z using a fill of 0. Finally in both solutions we reconstruct the rows from that by rbind'ing everything together. # 1 f <- function(x) { DF <- data.frame(A = 1:max(x$A), B = 0) DF[x$A,"B"] <- x$B DF } do.call(rbind, by(DF, cumsum(DF$A == 1), f)) # 2 library(zoo) f <- function(x) { z <- zoo(x$B, x$A) ser <- merge(zoo(,time(as.ts(z))), z, fill = 0) data.frame(A = time(ser), B = coredata(ser)) } do.call(rbind, by(DF, cumsum(DF$A == 1), f)) On 8/18/06, Sachin J <sachinj.2006@yahoo.com> wrote:Received on Sat Aug 19 03:57:21 2006

>

> Gabor,

>> Thanks a lot for the help. The 1st method works fine. In 2nd method I am> getting following error.>> > do.call(rbind, by(DF, cumsum(DF$A == 1), f))> Error in zoo(, time(as.ts(z)), z, fill = 0) :> unused argument(s) (fill ...)> Unable to figure out the cause.>> Thanks,> Sachin>>> Gabor Grothendieck <ggrothendieck@gmail.com> wrote:>> Here are two solutions. In both we break up DF into rows> which start with 1.>> In solution #1 we create a new data frame with the required sequence> for A and zeros for B and then we fill it in.>> In solution #2 we convert each set of rows to a zoo object z> where column A is the times and B is the data. We convert> that zoo object to a ts object (which has the effect of> filling in the missing times) and then create a zoo object> with no data from its times merging that zoo object with z> using a fill of 0.>> Finally in both solutions we reconstruct the rows from that by> rbind'ing everything together.>>> # 1> f <- function(x) {> DF <- data.frame(A = 1:max(x$A), B = 0)> DF[x$A,"B"] <- x$B> DF> }> do.call(rbind, by(DF, cumsum(DF$A == 1), f))>> # 2> library(zoo)> f <- function(x) {> z <- zoo(x$B, x$A)> ser <- merge(zoo(,time(as.ts(z)), z, fill = 0)> data.frame(A = time(ser), B = coredata(ser))> }> do.call(rbind, by(DF, cumsum(DF$A == 1), f)>>>>> On 8/18/06, Sachin J wrote:> > Hi,> >> > I have following dataframe. Column A indicates months.> >> > DF <- structure(list(A = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1,> > 2, 3, 4, 5, 7, 8, 11, 12, 1, 2, 3, 4, 5, 8), B = c(0, 0, 0, 8,> > 0, 19, 5, 19, 0, 0, 0, 11, 0, 8, 5, 11, 19, 8, 11, 10, 0, 8,> > 36, 10, 16, 10, 22)), .Names = c("A", "B"), class = "data.frame",> row.names = c("1",> > "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",> > "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",> > "25", "26", "27"))> >> > There is some discontinuity in the data. For example month 6, 9,10 data> (2nd year) and month 6 data (3rd year) are absent. I want to insert the rows> in place of these missing months and set the corresponding B column to zero.> i.e., the result should look like:> >> > DFNEW <- structure(list(A = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1,> > 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8),> > B = c(0, 0, 0, 8, 0, 19, 5, 19, 0, 0, 0, 11, 0, 8, 5, 11,> > 19, 0, 8, 11, 0, 0, 10, 0, 8, 36, 10, 16, 10, 0, 0, 22)), .Names = c("A",> > "B"), class = "data.frame", row.names = c("1", "2", "3", "4",> > "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",> > "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",> > "27", "28", "29", "30", "31", "32"))> >> > Thanks in advance.> >> > Sachin> >> >> > ---------------------------------> >> >> > [[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.> >>>>>> ________________________________> Do you Yahoo!?> Next-gen email? Have it all with the all-new Yahoo! Mail Beta.>>

______________________________________________ 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.

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 Sat 19 Aug 2006 - 04:21:40 EST.

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