【发布时间】:2021-12-06 12:28:38
【问题描述】:
我有一个包含一些日期的 df,我想过滤日期以仅显示当前月份和未来 12 个月。
对于Date 列中的每个日期,我想在DataReferencia 列中保留当前月份和未来12 个月的日期,然后从Value 列中减去这些值。对于上述日期,在 2003 年 1 月 17 日当天,它将是 DataReferencia 列 2003-01-01 和 2003-12-01 中的日期。此 df 从 2003-01 运行到 2020-12。
我试过这段代码,但返回一个空的df:
library(dplyr)
library(lubridate)
test %>%
filter(year(DataReferencia) == Data.Ano & month(DataReferencia) == Data.Mes + 11,
month(DataReferencia) == Data.Mes)
我的dput:
structure(list(Instituicao = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1), Data = structure(c(12069, 12069, 12069,
12069, 12069, 12069, 12069, 12069, 12069, 12069, 12069, 12069,
12070, 12070, 12070, 12070, 12070), class = "Date"), DataReferencia = structure(c(12053,
12084, 12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326,
12357, 12387, 12053, 12084, 12112, 12143, 12173), class = "Date"),
Valor = c(26, 24, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
26, 24, 22, 22, 22), DataReuniao = structure(c(12073, 12073,
12073, 12073, 12073, 12073, 12073, 12073, 12073, 12073, 12073,
12073, 12073, 12073, 12073, 12073, 12073), class = "Date"),
Reuniao = c(80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80), MetaSelic = c(25.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5)), row.names = c(NA, 17L), class = "data.frame")
【问题讨论】:
-
Data.Ano和Data.Mes的值是什么,它们来自哪里?请在您的示例中包含这些内容。