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

Marna Wagley m@rn@@w@g|ey @end|ng |rom gm@||@com
Fri May 3 21:24:09 CEST 2019

```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]]

```