Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在data.table中生成日期序列_R_Date_Data.table - Fatal编程技术网

R 在data.table中生成日期序列

R 在data.table中生成日期序列,r,date,data.table,R,Date,Data.table,我有一个相当简单的问题。见以下数据表(dt): 不幸的是,我只能通过和将一个值传递给。我有一个非常大的数据集,希望有一个快速简单的解决方案。您可以使用lubridate包和MONTH函数: dt$date2 <- seq(dt$date, by = paste(dt$Index ,"months"), length = 2)[2] library(lubridate) # First copye the date column dt$date2 <- dt$date # Then

我有一个相当简单的问题。见以下数据表(dt):

不幸的是,我只能通过和将一个值传递给
。我有一个非常大的数据集,希望有一个快速简单的解决方案。

您可以使用lubridate包和MONTH函数:

dt$date2 <- seq(dt$date, by = paste(dt$Index ,"months"), length = 2)[2]
library(lubridate)

# First copye the date column
dt$date2 <- dt$date
# Then apply MONTH function
month(dt$date2) <- month(dt$date2) + dt$Index
library(lubridate)

# First copye the date column
dt$date2 <- dt$date
# Then apply MONTH function
month(dt$date2) <- month(dt$date2) + dt$Index
    id       date Index      date2
1:   1 2000-01-01     0 2000-01-01
2:   1 2000-01-01     1 2000-02-01
3:   1 2000-01-01     2 2000-03-01
4:   1 2000-01-01     3 2000-04-01
5:   1 2000-01-01     4 2000-05-01
6:   2 2000-01-01     0 2000-01-01
7:   2 2000-01-01     1 2000-02-01
8:   2 2000-01-01     2 2000-03-01
9:   2 2000-01-01     3 2000-04-01
10:  2 2000-01-01     4 2000-05-01
11:  2 2000-01-01     5 2000-06-01
12:  2 2000-01-01     6 2000-07-01
13:  2 2000-01-01     7 2000-08-01
14:  2 2000-01-01     8 2000-09-01
15:  2 2000-01-01     9 2000-10-01