使用 lubridate 函数有条件地更改日期中的年份
Using lubridate function to conditionally change year in date
所以我目前正在努力使用 if 函数更改日期的年份部分。
我的日期列看起来像这样(真实数据框中有更多的值,所以不能手动解决):
Date
<S3: POSIXct>
2020-10-31
2020-12-31
我基本上想创建一个 if 命令(或任何其他方法),如果月份小于 12(即不是 12 月),则年份增加 1。
到目前为止,我已经尝试使用 lubridate 包来做:
if (month(df$Date)<12) {
df$Date <- df$Date %m+% years(1)
}
然而,该函数似乎没有正确调节,而是每年增加一个,包括月份 =12
的年份
如有任何帮助,我们将不胜感激!
你可以这样处理:
library(lubridate)
dates <- seq.Date(from = as.Date('2015-01-01'),
to = Sys.Date(),
by = 'months')
indicator <- month(dates) < 12
dates2 <- dates
dates2[indicator] <- dates[indicator] %m+% years(1)
res <- data.frame(dates, indicator, dates2)
> head(res, 25)
dates indicator dates2
1 2015-01-01 TRUE 2016-01-01
2 2015-02-01 TRUE 2016-02-01
3 2015-03-01 TRUE 2016-03-01
4 2015-04-01 TRUE 2016-04-01
5 2015-05-01 TRUE 2016-05-01
6 2015-06-01 TRUE 2016-06-01
7 2015-07-01 TRUE 2016-07-01
8 2015-08-01 TRUE 2016-08-01
9 2015-09-01 TRUE 2016-09-01
10 2015-10-01 TRUE 2016-10-01
11 2015-11-01 TRUE 2016-11-01
12 2015-12-01 FALSE 2015-12-01
13 2016-01-01 TRUE 2017-01-01
14 2016-02-01 TRUE 2017-02-01
15 2016-03-01 TRUE 2017-03-01
16 2016-04-01 TRUE 2017-04-01
17 2016-05-01 TRUE 2017-05-01
18 2016-06-01 TRUE 2017-06-01
19 2016-07-01 TRUE 2017-07-01
20 2016-08-01 TRUE 2017-08-01
21 2016-09-01 TRUE 2017-09-01
22 2016-10-01 TRUE 2017-10-01
23 2016-11-01 TRUE 2017-11-01
24 2016-12-01 FALSE 2016-12-01
25 2017-01-01 TRUE 2018-01-01
找到了一个更直接的解决方案,它不使用 if 命令。
df$Date[month(df$Date)<12] <- df$Date[month(df$Date)<12] %m+% years(1)
这里还是使用了lubridate包中的month函数来获取条件
所以我目前正在努力使用 if 函数更改日期的年份部分。
我的日期列看起来像这样(真实数据框中有更多的值,所以不能手动解决):
Date
<S3: POSIXct>
2020-10-31
2020-12-31
我基本上想创建一个 if 命令(或任何其他方法),如果月份小于 12(即不是 12 月),则年份增加 1。
到目前为止,我已经尝试使用 lubridate 包来做:
if (month(df$Date)<12) {
df$Date <- df$Date %m+% years(1)
}
然而,该函数似乎没有正确调节,而是每年增加一个,包括月份 =12
的年份如有任何帮助,我们将不胜感激!
你可以这样处理:
library(lubridate)
dates <- seq.Date(from = as.Date('2015-01-01'),
to = Sys.Date(),
by = 'months')
indicator <- month(dates) < 12
dates2 <- dates
dates2[indicator] <- dates[indicator] %m+% years(1)
res <- data.frame(dates, indicator, dates2)
> head(res, 25)
dates indicator dates2
1 2015-01-01 TRUE 2016-01-01
2 2015-02-01 TRUE 2016-02-01
3 2015-03-01 TRUE 2016-03-01
4 2015-04-01 TRUE 2016-04-01
5 2015-05-01 TRUE 2016-05-01
6 2015-06-01 TRUE 2016-06-01
7 2015-07-01 TRUE 2016-07-01
8 2015-08-01 TRUE 2016-08-01
9 2015-09-01 TRUE 2016-09-01
10 2015-10-01 TRUE 2016-10-01
11 2015-11-01 TRUE 2016-11-01
12 2015-12-01 FALSE 2015-12-01
13 2016-01-01 TRUE 2017-01-01
14 2016-02-01 TRUE 2017-02-01
15 2016-03-01 TRUE 2017-03-01
16 2016-04-01 TRUE 2017-04-01
17 2016-05-01 TRUE 2017-05-01
18 2016-06-01 TRUE 2017-06-01
19 2016-07-01 TRUE 2017-07-01
20 2016-08-01 TRUE 2017-08-01
21 2016-09-01 TRUE 2017-09-01
22 2016-10-01 TRUE 2017-10-01
23 2016-11-01 TRUE 2017-11-01
24 2016-12-01 FALSE 2016-12-01
25 2017-01-01 TRUE 2018-01-01
找到了一个更直接的解决方案,它不使用 if 命令。
df$Date[month(df$Date)<12] <- df$Date[month(df$Date)<12] %m+% years(1)
这里还是使用了lubridate包中的month函数来获取条件