如何创建连续的列 (R)

How to create consecutive columns (R)

我有处方记录数据,想知道从开具日期到记录结束,每个人每年有多少处方。示例数据(每个 ID 的前 5 行):

     ID Issue_Date index.date other.drugs
  1:  1 2000-02-08 2011-02-03           1
  2:  1 2000-04-04 2011-02-03           0
  3:  1 2000-05-30 2011-02-03           1
  4:  1 2000-07-25 2011-02-03           1
  5:  1 2000-08-22 2011-02-03           1
 ---                                     
  1:  2 2007-03-23 2009-04-03           1
  2:  2 2007-04-04 2009-04-03           1
  3:  2 2007-04-23 2009-04-03           1
  4:  2 2007-04-23 2009-04-03           0
  5:  2 2007-05-21 2009-04-03           1

other.drugs 列是一个指示变量,显示在该日期开出的处方是否不是研究中感兴趣的处方。 index.date 是他们进入研究的日期。 ID有1000多个,这里只给出2个

我想在他们 issue.date 之后的每一年中找到每年 other.drugs 的总和。我使用以下代码分别计算了第一年的费用:

dt <- dt[, yearend.1 := Issue_Date[1]+365, by = ID]
dt <- dt[(Issue_Date<=yearend.1), comorbid.1 := sum(other.drugs), by = ID]
dt <- dt[, comorbid.1:= comorbid.1[!is.na(comorbid.1)][1], by = ID]
# the last line copies the value to each cell the ID occupies in the data.table for that column instead of having NA's

结果如下:

     ID Issue_Date index.date other.drugs  yearend.1 comorbid.1
  1:  1 2000-02-08 2011-02-03           1 2001-02-07          8
  2:  1 2000-04-04 2011-02-03           1 2001-02-07          8
  3:  1 2000-05-30 2011-02-03           1 2001-02-07          8
  4:  1 2000-07-25 2011-02-03           1 2001-02-07          8
  5:  1 2000-08-22 2011-02-03           1 2001-02-07          8
---
  1:  2 2007-03-23 2009-04-03           1 2008-03-22         30
  2:  2 2007-04-04 2009-04-03           1 2008-03-22         30
  3:  2 2007-04-23 2009-04-03           1 2008-03-22         30
  4:  2 2007-04-23 2009-04-03           1 2008-03-22         30
  5:  2 2007-05-21 2009-04-03           1 2008-03-22         30

解释:ID 1 在他们第一个 issue_date 后的一年里开了 8 种其他药物,ID 2 开了 30 种。

对于2-10年(最多有11年的记录)我写了下面的循环:

years <- seq(730, 3650, 365)
# number of days in 2-10 years.
years2 <- seq(2,10,1)
# numbering the years for column names
colnames <- paste0("yearend.", years2)
colnames2 <- paste0("comorbid.", years2)
# names of columns to be used

for (i in 1:length(years)) {
  dt <- dt[, colnames[i] := Issue_Date[1]+years[i], by = ID]
  dt <- dt[(Issue_Date>=(as.Date(colnames[i], "%d-%m-%Y")) & Issue_Date<(as.Date(colnames[i+1], "%d-%m-%Y"))), 
         colnames2[i] := sum(other.drugs), by = ID]
  dt <- dt[, colnames2[i]:= colnames2[i][!is.na(colnames2[i])][1], by = ID]
}

然而,应该创建的新列是:

     ID Issue_Date index.date other.drugs  yearend.1 comorbid.1  yearend.2 comorbid.2  yearend.3 comorbid.3
  1:  1 2000-02-08 2011-02-03           1 2001-02-07          8 2002-02-07 comorbid.2 2003-02-07 comorbid.3
  2:  1 2000-04-04 2011-02-03           1 2001-02-07          8 2002-02-07 comorbid.2 2003-02-07 comorbid.3 
  3:  1 2000-05-30 2011-02-03           1 2001-02-07          8 2002-02-07 comorbid.2 2003-02-07 comorbid.3
  4:  1 2000-07-25 2011-02-03           1 2001-02-07          8 2002-02-07 comorbid.2 2003-02-07 comorbid.3
  5:  1 2000-08-22 2011-02-03           1 2001-02-07          8 2002-02-07 comorbid.2 2003-02-07 comorbid.3 
 ---

我想知道我的循环出了什么问题。非常感谢帮助。

每当您需要在 data.table 中使用实际上来自 R 中变量的列名时,您需要使用 get。因此你应该像这样重写你的循环,

for (i in 1:length(years)) {
  dt <- dt[, colnames[i] := Issue_Date[1]+years[i], by = ID]
  dt <- dt[(Issue_Date>=(as.Date(get(colnames[i]), "%d-%m-%Y")) & Issue_Date<(as.Date(get(colnames[i+1]), "%d-%m-%Y"))), 
         colnames2[i] := sum(other.drugs), by = ID]
  dt <- dt[, colnames2[i]:= get(colnames2[i])[!is.na(get(colnames2[i]))][1], by = ID]
}

我无法按原样实际测试您的代码,因为我遇到了 2 个问题:

  • 我没有足够的数据,所以我无法从你的时间条件中得到任何东西Issue_Date>...
  • 也许我遗漏了一些东西,但在你的循环中你试图使用 colnames[i+1],即 yearend.X 在它实际创建之前(也许你已经 运行 它几个次,这就是为什么你没有收到错误?)

我做了这样的事情来测试它,当然 comorbid.2 的值没有意义:

dt
    ID Issue_Date index.date other.drugs yearend.1 comorbid.1
 1:  1   00-02-08 2011-02-03           1  01-02-07          4
 2:  1   00-04-04 2011-02-03           0  01-02-07          4
 3:  1   00-05-30 2011-02-03           1  01-02-07          4
 4:  1   00-07-25 2011-02-03           1  01-02-07          4
 5:  1   00-08-22 2011-02-03           1  01-02-07          4
 6:  2   07-03-23 2009-04-03           1  08-03-22          4
 7:  2   07-04-04 2009-04-03           1  08-03-22          4
 8:  2   07-04-23 2009-04-03           1  08-03-22          4
 9:  2   07-04-23 2009-04-03           0  08-03-22          4
10:  2   07-05-21 2009-04-03           1  08-03-22          4

i <- 1
dt <- dt[, colnames[i] := Issue_Date[1]+years[i], by = ID]
dt <- dt[Issue_Date<get(colnames[i]), 
         colnames2[i] := sum(other.drugs), by = ID]
dt <- dt[, colnames2[i]:= get(colnames2[i])[!is.na(get(colnames2[i]))][1], by = ID]

dt
    ID Issue_Date index.date other.drugs yearend.1 comorbid.1 yearend.2 comorbid.2
 1:  1   00-02-08 2011-02-03           1  01-02-07          4  02-02-07          4
 2:  1   00-04-04 2011-02-03           0  01-02-07          4  02-02-07          4
 3:  1   00-05-30 2011-02-03           1  01-02-07          4  02-02-07          4
 4:  1   00-07-25 2011-02-03           1  01-02-07          4  02-02-07          4
 5:  1   00-08-22 2011-02-03           1  01-02-07          4  02-02-07          4
 6:  2   07-03-23 2009-04-03           1  08-03-22          4  09-03-22          4
 7:  2   07-04-04 2009-04-03           1  08-03-22          4  09-03-22          4
 8:  2   07-04-23 2009-04-03           1  08-03-22          4  09-03-22          4
 9:  2   07-04-23 2009-04-03           0  08-03-22          4  09-03-22          4
10:  2   07-05-21 2009-04-03           1  08-03-22          4  09-03-22          4

希望对您有所帮助。