根据与 r 中其他数据帧中日期的差异将日期转换为时间序列

Converting dates into time series based on difference from date in other dataframe in r

已编辑:澄清每个 id

可能有多个 A04

我需要获取前 6 个月的所有注册,这些注册分别针对每个案例。

我想设置一个人为的时间线。所以所有案例 id 都有自己的时间表。起点是第一行x=A=04,然后计算天数back/forward,按天数选择。注意:这是一个巨大的数据集,每个 id.

可以有多个 x=A04

我的数据集看起来像这样:

    id x   date
 1: 12 D95 2015-06-19
 2: 12 A04 2015-08-15
 3: 12 A01 2015-03-16
 4: 12 A04 2015-12-20
 5: 10 K20 2017-02-20
 6: 10 B10 2017-09-01
 7: 10 A04 2017-12-11
 8: 10 A84 2017-10-11

数据:(根据建议编辑jay.sf)

df <- structure(list(id = c(12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L), 
    x = c("D95", "A04", "A01", "A04", "K20", "B10", "A04", "A84"
    ), date = structure(c(16605, 16510, 16455, 17217, 17410, 
    17511, 17450, NA), class = "Date")), row.names = c(NA, -8L
), class = "data.frame")

我有一个单独的数据集 df_s,其中包含单行 idx=A04 中最早的 date。以为这会有所帮助,但现在我被卡住了...

单独数据集的示例 df_s:

    id date    
 1: 12 2015-08-15 
 2: 10 2017-12-11

数据(根据jay.sf的建议添加)

df_s <- structure(list(id = c(12L, 10L), date = structure(c(16789, 17511
), class = "Date")), row.names = c(NA, -2L), class = "data.frame") 

这将是我想要的新数据集(天数未精确计算):

    id x   date       days.since.first.A04
 1: 12 D95 2015-06-19 -98
 2: 12 A04 2015-08-15 0
 3: 12 A01 2015-03-16 -170
 4: 12 A04 2015-12-20 127
 5: 10 K20 2018-02-20 70
 6: 10 B10 2017-09-01 -101
 7: 10 A04 2017-12-11 0
 8: 10 A84 2017-10-11 -60

使用match和简单的减法。

df <- transform(df, days.since=df$date - df_s$date[match(df$id, df_s$id)])
#   id   x       date days.since
# 1 12 D95 2015-06-19  -184 days
# 2 12 F85 2015-03-16  -279 days
# 3 12 A01 2015-01-20  -334 days
# 4 12 A04 2017-02-20   428 days
# 5 10 K20 2017-09-01  -101 days
# 6 10 B10 2017-12-11     0 days
# 7 10 A04 2017-10-11   -61 days
# 8 10 A84       <NA>    NA days

编辑: 如果您的日期变量尚未采用 "Date" 格式,请使用 as.Date.

df <- transform(df, days.since=as.Date(df$date) - as.Date(df_s$date[match(df$id, df_s$id)]))

数据

df <- structure(list(id = c(12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L), 
    x = c("D95", "F85", "A01", "A04", "K20", "B10", "A04", "A84"
    ), date = structure(c(16605, 16510, 16455, 17217, 17410, 
    17511, 17450, NA), class = "Date")), row.names = c(NA, -8L
), class = "data.frame")

df_s <- structure(list(id = c(12L, 10L), date = structure(c(16789, 17511
), class = "Date")), row.names = c(NA, -2L), class = "data.frame")

使用data.table的选项:

setDT(DT, key=c("id","date"))[, date := as.IDate(date)]
DT[, days.since.first.A04 := unique(DT[x=="A04"], by="id")[.SD, on=.(id), i.date - x.date]]

输出:

   id   x       date days.since.first.A04
1: 10 B10 2017-09-01                 -101
2: 10 A84 2017-10-11                  -61
3: 10 A04 2017-12-11                    0
4: 10 K20 2018-02-20                   71
5: 12 A01 2015-03-16                 -279
6: 12 D95 2015-06-19                 -184
7: 12 F85 2015-08-15                 -127
8: 12 A04 2015-12-20                    0

数据:

library(data.table)
DT <- fread("id x   date
12 D95 2015-06-19
12 F85 2015-08-15
12 A01 2015-03-16
12 A04 2015-12-20
10 K20 2018-02-20
10 B10 2017-09-01
10 A04 2017-12-11
10 A84 2017-10-11")