[R] create a table for time difference (from t3 to t1, so on..)

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Fri May 3 23:57:52 CEST 2019


Hi Marna,
You can get the information you need with this:

dAT$date<-as.Date(dAT$date,"%d-%b-%y")
diffs<-function(x,maxn) return(diff(x)[1:maxn])
initdate<-function(x) return(min(x))
datediffs<-aggregate(dAT$date,list(dAT$Id),diffs,3)

I can't do the manipulation of the resulting values at the moment, but
can work it out later if necessary.

Jim

On Sat, May 4, 2019 at 5:24 AM Marna Wagley <marna.wagley using gmail.com> wrote:
>
> Hi R User.
> I have a date set in which I wanted to find the duration (period)  between
> released time and detection time for each individual. Is there any simplest
> way to create a matrix?
> I appreciate your help.
>
> Thanks,
> MW
> --------------
> Here is the example data,
>
> dAT<-structure(list(Id = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 5L, 4L,
>
> 1L, 6L, 6L, 3L, 4L, 1L), .Label = c("a", "b", "c", "e", "f",
>
> "m"), class = "factor"), date = structure(c(1L, 1L, 1L, 5L, 4L,
>
> 1L, 5L, 5L, 3L, 1L, 2L, 7L, 6L, 8L), .Label = c("1-Jan-18", "1-Jul-18",
>
> "10-Mar-18", "15-Jan-18", "2-Jan-18", "27-Jan-18", "3-Jan-18",
>
> "7-Mar-19"), class = "factor")), .Names = c("Id", "date"), class =
> "data.frame", row.names = c(NA,
>
> -14L))
>
>
> #and the results looks like
>
>
> Result<-structure(list(Id = structure(1:6, .Label = c("a", "b", "c",
>
> "e", "f", "m"), class = "factor"), intial.released.date = structure(c(1L,
>
> 1L, 1L, 2L, 2L, 1L), .Label = c("1-Jan-18", "2-Jan-18"), class = "factor"),
>
>     duration.between.t2.and.t1 = c(14L, 0L, 2L, 0L, NA, 181L),
>
>     duration.between.t3.and.t1 = c(68L, NA, NA, 25L, NA, NA),
>
>     duration.between.t4.and.t1 = c(430L, NA, NA, NA, NA, NA),
>
>     duration.between.t3.and.t2 = c(54L, NA, NA, NA, NA, NA),
>
>     duration.between.t4.and.t2 = c(416L, NA, NA, NA, NA, NA)), .Names = c(
> "Id",
>
> "intial.released.date", "duration.between.t2.and.t1",
> "duration.between.t3.and.t1",
>
> "duration.between.t4.and.t1", "duration.between.t3.and.t2",
> "duration.between.t4.and.t2"
>
> ), class = "data.frame", row.names = c(NA, -6L))
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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