[R] Insert rows - how can I accomplish this in R

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 18 15:38:54 CEST 2006


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 <sachinj.2006 at yahoo.com> 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 at 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.
>



More information about the R-help mailing list