根据第二个数据框按组在一个数据框中创建一系列日期

Create a sequence of dates in one data frame based on a second data frame by group

我有两个数据框共享一个分组 ID。我想根据另一个设置的条件在一个中创建一系列日期。 MRE如下:

jdates <- structure(list(Group.1 = c(8, 9), x = structure(c(16674, 16661), class = "Date")), .Names = c("Group.1", "x"), row.names = c(NA, -2L), class = c("data.table", "data.frame"))

jtrying <- structure(list(id = c(8, 8, 8, 9, 9, 9), values1 = 1:6, values2 = 7:12), .Names = c("id", "values1", "values2"), row.names = c(NA, -6L), class = c("data.table", "data.frame"))

在这个例子中,我想在 jtrying 中创建一个日期列,从 jdates 中的下一个日期开始(每组 - Group.1jdatesidjtrying).

在 data.table 中,none 这些(糟糕的)方法奏效了:

jtrying[ , date := seq(jdates$x + 1, length.out=3, by = 1), by = jdates$Group.1]

jtrying[ , date := seq(jdates$x + 1, length.out=3, by = 1), by = id]

jtrying[ , date := lapply(.SD,(seq(jdates$x + 1, length.out=3, by = 1))), by = id]

jtrying[ , date := lapply(.SD,function(x) seq(jdates$x + 1, length.out=3, by = 1)), by = id]

我一直在尝试 data.table 方法,因为它们被认为更快(并且实际数据非常大),但实际上,任何(在合理范围内)都可以做到。

我的预期结果是 data.frame,如下所示:

 jtrying
   id values1 values2       date
1:  8       1       7 2015-08-28
2:  8       2       8 2015-08-29
3:  8       3       9 2015-08-30
4:  9       4      10 2015-08-15
5:  9       5      11 2015-08-16
6:  9       6      12 2015-08-17

这是我会做的

jtrying[jdates, 
  date := seq(from=x+1, by=1, length.out=.N)
, on=c(id="Group.1"), by=.EACHI]

此语法 X[Y, newcol := ..., on=c(Xcol=Ycol), by=.EACHI] 的工作方式如下:

  • XYon 中标识的列上合并。
  • X 为合并列的每个值(即 by=.EACHI)分别构建其 newcol