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

arun smartpink111 at yahoo.com
Wed Oct 8 10:29:26 CEST 2014



If the `ids` are ordered as shown in the example, perhaps you need

   tbl <- table(df$id)
   
   rep(seq(as.Date("2000-01-01"), length.out=length(tbl), by=1), tbl)
    [1] "2000-01-01" "2000-01-01" "2000-01-01" "2000-01-01" "2000-01-01"
    [6] "2000-01-02" "2000-01-02" "2000-01-02" "2000-01-02" "2000-01-02"
    [11] "2000-01-03" "2000-01-03" "2000-01-03" "2000-01-03" "2000-01-03"
    [16] "2000-01-04" "2000-01-04" "2000-01-04" "2000-01-04" "2000-01-05"
    [21] "2000-01-05" "2000-01-05" "2000-01-05"

A.K.


On Wednesday, October 8, 2014 3: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.



More information about the R-help mailing list