Marna Wagley
2019-May-03 19:24 UTC
[R] create a table for time difference (from t3 to t1, so on..)
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]]
Jim Lemon
2019-May-03 21:57 UTC
[R] create a table for time difference (from t3 to t1, so on..)
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 at 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 at 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.
Jim Lemon
2019-May-04 09:48 UTC
[R] create a table for time difference (from t3 to t1, so on..)
Hi Marna,
I was able to have another look at it. I think this is what you want
except for the column names (I'm lazy):
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))
dAT$date<-as.Date(dAT$date,"%d-%b-%y")
diffs<-function(x,maxn) return(diff(x)[1:maxn])
mindate<-function(x) return(min(x))
datediffs<-aggregate(dAT$date,list(dAT$Id),diffs,3)
initdates<-as.character(as.Date(as.vector(by(dAT$date,dAT$Id,min)),
origin=as.Date("1970-01-01")))
Result<-data.frame(Id=datediffs$Group.1,Release=initdates,
'T1-T2'=datediffs$x[,1],'T1-T3'=datediffs$x[,1]+datediffs$x[,2],
'T1-T4'=datediffs$x[,1]+datediffs$x[,2]+datediffs$x[,3],
'T2-T3'=datediffs$x[,2],'T2-T4'=datediffs$x[,2]+datediffs$x[,3])
Jim
On Sat, May 4, 2019 at 7:57 AM Jim Lemon <drjimlemon at gmail.com>
wrote:>
> 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 at
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 at 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.