Generate sequence of date based on a group ID

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Generate sequence of date based on a group ID

Anuma
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))

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Generate sequence of date based on a group ID

arun kirshna


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 <[hidden email]> 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))

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Generate sequence of date based on a group ID

Adams, Jean
In reply to this post by Anuma
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 <[hidden email]> 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))
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.