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.wag...@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@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. ______________________________________________ R-help@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.