R从日常数据增加数月

时间:2017-02-16 16:03:44

标签: r

我有大约20年的日常数据,我使用month()函数提取月份。现在我希望这些月份在一年结束时不会改变为1(1月)。我希望他们继续前进13,14,15等等。 R中有功能吗?或者我该怎么做?

例如我有这个:

1993-01-04     1.953         1
1993-01-05     1.978         1
1993-01-06     2.007         1
1993-01-07     1.957         1
1993-01-08     1.955         1
1993-01-11     1.982         1
1993-01-12     1.958         1
1993-01-13     1.924         1
1993-01-14     1.887         1
1993-01-15     1.971         1
1993-01-19     2.098         1
1993-01-20     2.087         1
1993-01-21     2.062         1
1993-01-22     2.056         1
1993-01-25     2.051         1
1993-01-26     2.038         1
1993-01-27     1.990         1
1993-01-28     2.032         1
1993-01-29     2.027         1
1993-02-01     2.083         2
1993-02-02     2.027         2
1993-02-03     2.009         2
1993-02-04     2.058         2
1993-02-05     2.073         2
1993-02-08     2.071         2
1993-02-09     2.022         2
1993-02-10     2.077         2
1993-02-11     2.116         2
1993-02-12     2.049         2
1993-02-15     2.086         2
1993-02-16     2.072         2
1993-02-17     2.061         2
1993-02-18     2.109         2
1993-02-19     2.082         2
1993-02-23     2.027         2
1993-02-24     1.962         2
1993-02-25     1.966         2
1993-02-26     2.001         2
1993-03-01     2.027         3
1993-03-02     2.013         3
1993-03-03     1.989         3
1993-03-04     1.982         3
1993-03-05     1.949         3
1993-03-08     1.966         3
1993-03-09     1.987         3
1993-03-10     1.989         3
1993-03-11     2.009         3
1993-03-12     1.978         3
1993-03-15     1.970         3
1993-03-16     2.009         3
1993-03-17     1.938         3
1993-03-18     1.930         3
1993-03-19     1.914         3
1993-03-22     1.892         3
1993-03-23     1.902         3
1993-03-24     1.910         3
1993-03-25     1.897         3
1993-03-26     1.853         3
1993-03-29     1.879         3
1993-03-30     1.920         3
1993-03-31     1.906         3
1993-04-01     1.894         4
1993-04-02     1.901         4
1993-04-05     1.888         4
1993-04-07     1.905         4
1993-04-08     1.938         4
1993-04-13     1.935         4
1993-04-14     1.945         4
1993-04-15     1.926         4
1993-04-16     1.895         4
1993-04-19     1.885         4
1993-04-20     1.859         4
1993-04-21     1.858         4
1993-04-22     1.890         4
1993-04-23     1.848         4
1993-04-26     1.861         4
1993-04-27     1.837         4
1993-04-28     1.818         4
1993-04-29     1.812         4
1993-04-30     1.835         4
1993-05-03     1.893         5
1993-05-04     1.847         5
1993-05-05     1.841         5
1993-05-06     1.875         5
1993-05-07     1.885         5
1993-05-10     1.893         5
1993-05-11     1.891         5
1993-05-12     1.851         5
1993-05-13     1.889         5
1993-05-14     1.859         5
1993-05-17     1.827         5
1993-05-18     1.827         5
1993-05-19     1.803         5
1993-05-21     1.823         5
1993-05-24     1.812         5
1993-05-25     1.842         5
1993-05-26     1.783         5
1993-05-27     1.797         5
1993-05-28     1.784         5
1993-06-01     1.777         6
1993-06-02     1.774         6
1993-06-03     1.784         6
1993-06-04     1.804         6
1993-06-07     1.858         6
1993-06-08     1.868         6
1993-06-09     1.852         6
1993-06-11     1.888         6
1993-06-14     1.980         6
1993-06-15     1.972         6
1993-06-16     1.972         6
1993-06-17     2.018         6
1993-06-18     2.018         6
1993-06-21     2.075         6
1993-06-22     2.111         6
1993-06-23     2.140         6
1993-06-24     2.175         6
1993-06-25     2.174         6
1993-06-28     2.192         6
1993-06-29     2.175         6
1993-06-30     2.145         6
1993-07-01     2.104         7
1993-07-02     2.069         7
1993-07-05     2.076         7
1993-07-06     2.102         7
1993-07-07     2.174         7
1993-07-08     2.372         7
1993-07-09     2.402         7
1993-07-12     2.392         7
1993-07-13     2.378         7
1993-07-14     2.379         7
1993-07-15     2.381         7
1993-07-16     2.358         7
1993-07-19     2.413         7
1993-07-20     2.403         7
1993-07-21     2.373         7
1993-07-22     2.423         7
1993-07-23     2.393         7
1993-07-26     2.423         7
1993-07-27     2.382         7
1993-07-28     2.354         7
1993-07-29     2.314         7
1993-07-30     2.240         7
1993-08-02     2.272         8
1993-08-03     2.256         8
1993-08-04     2.282         8
1993-08-05     2.262         8
1993-08-06     2.288         8
1993-08-09     2.296         8
1993-08-10     2.264         8
1993-08-11     2.296         8
1993-08-12     2.333         8
1993-08-13     2.353         8
1993-08-16     2.373         8
1993-08-17     2.408         8
1993-08-18     2.398         8
1993-08-19     2.382         8
1993-08-20     2.368         8
1993-08-23     2.332         8
1993-08-24     2.362         8
1993-08-25     2.358         8
1993-08-26     2.286         8
1993-08-27     2.286         8
1993-08-30     2.313         8
1993-08-31     2.359         8
1993-09-01     2.293         9
1993-09-02     2.313         9
1993-09-03     2.283         9
1993-09-06     2.253         9
1993-09-07     2.237         9
1993-09-08     2.255         9
1993-09-09     2.279         9
1993-09-10     2.258         9
1993-09-13     2.253         9
1993-09-14     2.238         9
1993-09-15     2.238         9
1993-09-16     2.223         9
1993-09-17     2.258         9
1993-09-20     2.266         9
1993-09-21     2.243         9
1993-09-22     2.252         9
1993-09-23     2.303         9
1993-09-24     2.260         9
1993-09-27     2.317         9
1993-09-28     2.283         9
1993-09-29     2.298         9
1993-09-30     2.313         9
1993-10-01     2.315        10
1993-10-04     2.307        10
1993-10-05     2.398        10
1993-10-06     2.508        10
1993-10-07     2.386        10

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

test = c(seq(1,12,by=1),seq(1,12,by=1))
[1]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12
data.table::rleid(test)
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

这可能是一个更完整的答案。

library(data.table)
dt = data.table("Date" = seq.Date(as.Date("2014-12-21"),as.Date("2015-01-01"),by = 1), value = runif(12))
dt[, "Month" := month(Date)]
dt[, "ID" := rleid(Month)]

答案 1 :(得分:0)

我们可以使用base R执行此操作(假设日期已订购)

df1$index <- cumsum(c(TRUE, diff(df1$Month) < 0))