[R] Generate sequence of date based on a group ID

Adams, Jean jvadams at usgs.gov
Wed Oct 8 15:52:20 CEST 2014


Does this work for you?

df$in1 <- 0
df$in1[match(unique(df$id), df$id)] <- 1
df$date <- as.Date("2000-01-01") + cumsum(df$in1) - 1

Jean

On Wed, Oct 8, 2014 at 2:57 AM, Kuma Raj <pollaroid at gmail.com> wrote:

> I want to generate a sequence of date based on a group id(similar IDs
> should have same date). The id variable contains unequal observations
> and the length of the data set also varies.  How could I create a
> sequence that starts on specific date (say January 1, 2000 onwards)
> and continues until the end without specifying length?
>
>
> Sample data follows:
>
> df<-structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
>
> 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), out1 = c(0L,
>
> 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
>
> 0L, 1L, 0L, 0L, 0L, 1L)), .Names = c("id", "out1"), class =
> "data.frame", row.names = c(NA,
>
> -23L))
>
> ______________________________________________
> R-help at r-project.org 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list