[R] Split rows depending on time frame

Gabor Grothendieck ggrothendieck at gmail.com
Mon Oct 11 12:21:14 CEST 2010


On Mon, Oct 11, 2010 at 5:25 AM, Bert Jacobs
<bert.jacobs at figurestofacts.be> wrote:
> Hi,
>
>
>
> I have the following data frame, where col2 is a startdate and col3 an
> enddate
>
>
>
> COL1      COL2      COL3
>
> A             40462    40482
>
> B             40462    40478
>
>
>
> The above timeframe of 3 weeks I would like to splits it in weeks like this
>
> COL1      COL2      COL3      COL4
>
> A             40462    40468    1
>
> A             40469    40475    1
>
> A             40476    40482    1
>
> B             40462    40468    1
>
> B             40469    40475    1
>
> B             40476    40478    0.428
>
>
>
> Where COL4 is an identifier if the timeframe between COL2 and COL3 is
> exactly 7 days or shorter.
>
> In the example above for B the last split contains only 3 days so the value
> in COL 4 is 3/7

Try this:

DF <- data.frame(COL1 = c("A", "B"), COL2 = 40462, COL3 = c(40482, 40478))

do.call("rbind", by(DF, DF$COL1, function(x) with(x, {
	COL2 <- seq(COL2, COL3, 7)
	COL3 <- pmin(COL2 + 6, COL3)
	COL4 <- (COL3 - COL2 + 1) / 7
	data.frame(COL1, COL2, COL3, COL4)
})))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list