library(dplyr)
dat <- readr::read_table(
"sq_id ageclass sex cohort year age grid trialnumber trialdate trialtime
6244 A F 2000 2005 5 AG 1 05/24/05 0:00
10212 A M 2006 2008 2 KL 1 05/04/08 6:13
10212 A M 2006 2010 4 KL 4 05/20/10 6:12
10212 A M 2006 2009 3 KL 2 06/10/09 6:14
10212 A M 2006 2009 3 KL 3 07/01/09 6:15
23052 J F 2017 2017 0 SU 2 08/02/17 11:00
23052 J F 2017 2017 0 SU 1 07/20/17 10:51
23080 J M 2017 2017 0 KL 2 07/29/17 10:20
23080 J M 2017 2017 0 KL 1 07/07/17 8:35")
glimpse(dat)
# Observations: 9
# Variables: 9
# $ sq_id <int> 6244, 10212, 10212, 10212, 10212, 23052, 23052, 23080, 23080
# $ ageclass <chr> "A", "A", "A", "A", "A", "J", "J", "J", "J"
# $ sex <chr> "F", "M", "M", "M", "M", "F", "F", "M", "M"
# $ `cohort year` <chr> "2000 2005", "2006 2008", "2006 2010", "2006 2009", "2006 2009", "2017 2017", "2017 2017",...
# $ age <int> 5, 2, 4, 3, 3, 0, 0, 0, 0
# $ grid <chr> "AG", "KL", "KL", "KL", "KL", "SU", "SU", "KL", "KL"
# $ trialnumber <int> 1, 1, 4, 2, 3, 2, 1, 2, 1
# $ trialdate <chr> "05/24/05", "05/04/08", "05/20/10", "06/10/09", "07/01/09", "08/02/17", "07/20/17", "07/29...
# $ trialtime <time> 00:00:00, 06:13:00, 06:12:00, 06:14:00, 06:15:00, 11:00:00, 10:51:00, 10:20:00, 08:35:00
Кажется, нет необходимости отдельно рассчитывать промежутки времени, поэтому нет необходимости присоединяться:
dat %>%
mutate(trial_dt = lubridate::mdy_hms(paste(trialdate, trialtime))) %>%
group_by(sq_id) %>%
mutate(time_gap = difftime(trial_dt, min(trial_dt), units = "days"))
# # A tibble: 9 x 11
# # Groups: sq_id [4]
# sq_id ageclass sex `cohort year` age grid trialnumber trialdate trialtime trial_dt time_gap
# <int> <chr> <chr> <chr> <int> <chr> <int> <chr> <time> <dttm> <time>
# 1 6244 A F 2000 2005 5 AG 1 05/24/05 00:00 2005-05-24 00:00:00 0
# 2 10212 A M 2006 2008 2 KL 1 05/04/08 06:13 2008-05-04 06:13:00 0
# 3 10212 A M 2006 2010 4 KL 4 05/20/10 06:12 2010-05-20 06:12:00 745.999305555556
# 4 10212 A M 2006 2009 3 KL 2 06/10/09 06:14 2009-06-10 06:14:00 402.000694444444
# 5 10212 A M 2006 2009 3 KL 3 07/01/09 06:15 2009-07-01 06:15:00 423.001388888889
# 6 23052 J F 2017 2017 0 SU 2 08/02/17 11:00 2017-08-02 11:00:00 13.00625
# 7 23052 J F 2017 2017 0 SU 1 07/20/17 10:51 2017-07-20 10:51:00 0
# 8 23080 J M 2017 2017 0 KL 2 07/29/17 10:20 2017-07-29 10:20:00 22.0729166666667
# 9 23080 J M 2017 2017 0 KL 1 07/07/17 08:35 2017-07-07 08:35:00 0