如何在连接数据集时删除重复的观察

时间:2018-05-18 16:44:07

标签: r datetime merge dplyr left-join

我正在尝试计算每个人(trialnumber)的试验之间的时间量(sq_id)。我已经能够弄清楚如何计算试验之间的时差(time_gap),但我的输出中包含了不应该存在的所有重复行。

我的数据的一部分可以找到here。出于可重复性的目的,我在下面包含了数据集(称为export):

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

我做的第一件事是计算试验之间的时间,如下:

#adding time between trials to data
trialdate<-as.POSIXct(data$trialdate,format="%m/%d/%y")
data$datetime=as.POSIXct(paste(trialdate, data$trialtime),format= '%Y-%m-%d',usetz=FALSE)

#calculates time btw first trial and all other trials
timebtw <- data %>% group_by(sq_id) %>% 
    select(sq_id, trialnumber, datetime) %>%
    mutate(time_gap = (datetime - nth(datetime, which.min((datetime)))), time_gap=time_gap/86400) #time_gap units are in seconds, changed to days

然后我将timebtw数据集加入我的原始数据集(称为export):

new<-dplyr::left_join(export, timebtw, by = "sq_id") 

我得到的输出如下:

> export
sq_id ageclass sex cohort year age grid trialnumber.x trialdate trialtime   datetime time_gap trialnumber.y
6244         A   F   2000 2005   5   AG             1  05/24/05      0:00 2005-05-24   0 secs             1
10212        A   M   2006 2008   2   KL             1  05/04/08      6:13 2008-05-04   0 secs             1
10212        A   M   2006 2008   2   KL             1  05/04/08      6:13 2008-05-04 746 secs             4
10212        A   M   2006 2008   2   KL             1  05/04/08      6:13 2008-05-04 402 secs             2
10212        A   M   2006 2008   2   KL             1  05/04/08      6:13 2008-05-04 423 secs             3
10212        A   M   2006 2010   4   KL             4  05/20/10      6:12 2010-05-20   0 secs             1
10212        A   M   2006 2010   4   KL             4  05/20/10      6:12 2010-05-20 746 secs             4
10212        A   M   2006 2010   4   KL             4  05/20/10      6:12 2010-05-20 402 secs             2
10212        A   M   2006 2010   4   KL             4  05/20/10      6:12 2010-05-20 423 secs             3
10212        A   M   2006 2009   3   KL             2  06/10/09      6:14 2009-06-10   0 secs             1
10212        A   M   2006 2009   3   KL             2  06/10/09      6:14 2009-06-10 746 secs             4
10212        A   M   2006 2009   3   KL             2  06/10/09      6:14 2009-06-10 402 secs             2
10212        A   M   2006 2009   3   KL             2  06/10/09      6:14 2009-06-10 423 secs             3
10212        A   M   2006 2009   3   KL             3  07/01/09      6:15 2009-07-01   0 secs             1
10212        A   M   2006 2009   3   KL             3  07/01/09      6:15 2009-07-01 746 secs             4
10212        A   M   2006 2009   3   KL             3  07/01/09      6:15 2009-07-01 402 secs             2
10212        A   M   2006 2009   3   KL             3  07/01/09      6:15 2009-07-01 423 secs             3
23052        J   F   2017 2017   0   SU             2  08/02/17     11:00 2017-08-02  13 secs             2
23052        J   F   2017 2017   0   SU             2  08/02/17     11:00 2017-08-02   0 secs             1
23052        J   F   2017 2017   0   SU             1  07/20/17     10:51 2017-07-20  13 secs             2
23052        J   F   2017 2017   0   SU             1  07/20/17     10:51 2017-07-20   0 secs             1
23080        J   M   2017 2017   0   KL             2  07/29/17     10:20 2017-07-29  22 secs             2
23080        J   M   2017 2017   0   KL             2  07/29/17     10:20 2017-07-29   0 secs             1
23080        J   M   2017 2017   0   KL             1  07/07/17      8:35 2017-07-07  22 secs             2
23080        J   M   2017 2017   0   KL             1  07/07/17      8:35 2017-07-07   0 secs             1

这是一个问题。每time_gap只应有一个trialnumber值。

因此,例如,对于sq_id 10212,输出应如下所示:

sq_id ageclass sex cohort year age grid trialnumber.x trialdate trialtime   datetime time_gap trialnumber.y
10212        A   M   2006 2008   2   KL             1  05/04/08      6:13 2008-05-04   0 secs             1
10212        A   M   2006 2010   4   KL             4  05/20/10      6:12 2010-05-20 746 secs             4
10212        A   M   2006 2009   3   KL             2  06/10/09      6:14 2009-06-10 402 secs             2
10212        A   M   2006 2009   3   KL             3  07/01/09      6:15 2009-07-01 423 secs             3

我需要trialnumber.xtrialnumber.y列匹配,这样只有与试验一样多的行(即sq_id 6244将有1行,{{1 10212 4行,sq_id 23052 2行,sq_id 23080 2行)。

有谁知道如何修改我的代码才能获得此输出?

1 个答案:

答案 0 :(得分:0)

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  

似乎没有必要单独计算时间间隔,因此无需加入:

{{1}}