2016-01-21 39 views
1

生成日期的順序比方說,我們有以下data.table如何通過

set.seed(7) 
library(data.table) 
library(zoo) 
dt <- data.table(ID=c('a','a','a','b','b'), Tag=c(1,2,3,1,2), Begin=c('2015-01-01', '2014-05-07', '2014-08-02', '2015-02-03','2013-08-09'), x=rnorm(5), y = rnorm(5), z = rnorm(5)) 
dt[,Begin:=as.Date(Begin, '%Y-%m-%d')] 

回報,

ID Tag  Begin   x   y   z 
1: a 1 2015-01-01 2.2872472 -0.9472799 0.3569862 
2: a 2 2014-05-07 -1.1967717 0.7481393 2.7167518 
3: a 3 2014-08-02 -0.6942925 -0.1169552 2.2814519 
4: b 1 2015-02-03 -0.4122930 0.1526576 0.3240205 
5: b 2 2013-08-09 -0.9706733 2.1899781 1.8960671 

我有Begin列日,並希望延長Begin到未來2個月。我申請了下面的代碼:

dt[, Date := seq(from = Begin, to = Begin+months(2), by = '1 months'), by = .(ID, Tag)] 

,但我有以下錯誤:

Warning messages: 
1: In `[.data.table`(dt, , `:=`(Date, seq(from = Begin, : 
    RHS 1 is length 3 (greater than the size (1) of group 1). The last 2 element(s) will be discarded. 
2: In `[.data.table`(dt, , `:=`(Date, seq(from = Begin, : 
    RHS 1 is length 3 (greater than the size (1) of group 2). The last 2 element(s) will be discarded. 
3: In `[.data.table`(dt, , `:=`(Date, seq(from = Begin, : 
    RHS 1 is length 3 (greater than the size (1) of group 3). The last 2 element(s) will be discarded. 
4: In `[.data.table`(dt, , `:=`(Date, seq(from = Begin, : 
    RHS 1 is length 3 (greater than the size (1) of group 4). The last 2 element(s) will be discarded. 
5: In `[.data.table`(dt, , `:=`(Date, seq(from = Begin, : 
    RHS 1 is length 3 (greater than the size (1) of group 5). The last 2 element(s) will be discarded. 

,我所期望的結果是,

ID Tag  Date   x   y   z 
1: a 1 2015-01-01 2.2872472 -0.9472799 0.3569862 
2: a 1 2015-02-01 2.2872472 -0.9472799 0.3569862 
3: a 1 2015-03-01 2.2872472 -0.9472799 0.3569862 
4: a 2 2014-05-07 -1.1967717 0.7481393 2.7167518 
5: a 2 2014-06-07 -1.1967717 0.7481393 2.7167518 
6: a 2 2014-07-07 -1.1967717 0.7481393 2.7167518 
7: a 3 2014-08-02 -0.6942925 -0.1169552 2.2814519 
8: a 3 2014-09-02 -0.6942925 -0.1169552 2.2814519 
9: a 3 2014-10-02 -0.6942925 -0.1169552 2.2814519 
10: b 1 2015-02-03 -0.4122930 0.1526576 0.3240205 
11: b 1 2015-03-03 -0.4122930 0.1526576 0.3240205 
12: b 1 2015-04-03 -0.4122930 0.1526576 0.3240205 
13: b 2 2013-08-09 -0.9706733 2.1899781 1.8960671 
14: b 2 2013-09-09 -0.9706733 2.1899781 1.8960671 
15: b 2 2013-10-09 -0.9706733 2.1899781 1.8960671 

我猜發生了錯誤,因爲我可能不有獨特的鑰匙。

請注意,在我的示例數據中只有x,yz,但在我的實際數據集中,我有超過10列。

您能否給我建議?

+1

你可以使用'1:nrow(dt)'作爲分組變量。也轉換爲'Date'類 – akrun

回答

2

我們按行的順序進行分組,因爲「ID」,「標記」組有重複的元素。

dt[, list(Date = seq(Begin, length.out=3, by = '1 month'), x,y,z), by = 1:nrow(dt)] 

或者作爲@大衛Arenburg提到的,我們用「N」,然後按「ID」,「標籤」通過只選擇先觀察複製行「開始」

dt[rep(1:.N, each = 3)][, Begin := seq(Begin[1L], 
     length.out=3, by = '1 month'), by = .(ID, Tag)][]