使用回滚和lubridate获得NA值

时间:2019-07-25 20:46:38

标签: r lubridate

以下mutate函数在哪里出问题?

我想将日期回滚到月底,但是对于某些月份,它只返回NA值...

          date      date2
1   2014-01-31       <NA>
2   2014-02-28 2014-02-28
3   2014-03-31       <NA>
4   2014-04-30 2014-04-30
5   2014-05-30 2014-05-31
6   2014-06-30 2014-06-30
7   2014-07-31 2014-07-31
8   2014-08-29 2014-08-31
9   2014-09-30 2014-09-30
10  2014-10-31       <NA>

它只是在每年的1月,3月...创建NA。

代码:

library(lubridate)
library(dplyr)
mydates %>%
  mutate(date2 = rollback(ymd(parse_date_time(date, "%Y%m%d") + months(1))))

数据:

mydates <- structure(list(date = structure(c(109L, 110L, 111L, 112L, 113L, 
114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 
125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 
123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 109L, 
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 
121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 
132L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 
119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 
130L, 131L, 132L, 109L, 110L, 111L, 112L), .Label = c("2005-01-31", 
"2005-02-28", "2005-03-31", "2005-04-29", "2005-05-31", "2005-06-30", 
"2005-07-29", "2005-08-31", "2005-09-30", "2005-10-31", "2005-11-30", 
"2005-12-30", "2006-01-31", "2006-02-28", "2006-03-31", "2006-04-28", 
"2006-05-31", "2006-06-30", "2006-07-31", "2006-08-31", "2006-09-29", 
"2006-10-31", "2006-11-30", "2006-12-29", "2007-01-31", "2007-02-28", 
"2007-03-30", "2007-04-30", "2007-05-31", "2007-06-29", "2007-07-31", 
"2007-08-31", "2007-09-28", "2007-10-31", "2007-11-30", "2007-12-31", 
"2008-01-31", "2008-02-29", "2008-03-31", "2008-04-30", "2008-05-30", 
"2008-06-30", "2008-07-31", "2008-08-29", "2008-09-30", "2008-10-31", 
"2008-11-28", "2008-12-31", "2009-01-30", "2009-02-27", "2009-03-31", 
"2009-04-30", "2009-05-29", "2009-06-30", "2009-07-31", "2009-08-31", 
"2009-09-30", "2009-10-30", "2009-11-30", "2009-12-31", "2010-01-29", 
"2010-02-26", "2010-03-31", "2010-04-30", "2010-05-28", "2010-06-30", 
"2010-07-30", "2010-08-31", "2010-09-30", "2010-10-29", "2010-11-30", 
"2010-12-31", "2011-01-31", "2011-02-28", "2011-03-31", "2011-04-29", 
"2011-05-31", "2011-06-30", "2011-07-29", "2011-08-31", "2011-09-30", 
"2011-10-31", "2011-11-30", "2011-12-30", "2012-01-31", "2012-02-29", 
"2012-03-30", "2012-04-30", "2012-05-31", "2012-06-29", "2012-07-31", 
"2012-08-31", "2012-09-28", "2012-10-31", "2012-11-30", "2012-12-31", 
"2013-01-31", "2013-02-28", "2013-03-28", "2013-04-30", "2013-05-31", 
"2013-06-28", "2013-07-31", "2013-08-30", "2013-09-30", "2013-10-31", 
"2013-11-29", "2013-12-31", "2014-01-31", "2014-02-28", "2014-03-31", 
"2014-04-30", "2014-05-30", "2014-06-30", "2014-07-31", "2014-08-29", 
"2014-09-30", "2014-10-31", "2014-11-28", "2014-12-31", "2015-01-30", 
"2015-02-27", "2015-03-31", "2015-04-30", "2015-05-29", "2015-06-30", 
"2015-07-31", "2015-08-31", "2015-09-30", "2015-10-30", "2015-11-30", 
"2015-12-31", "2016-01-29", "2016-02-29", "2016-03-31", "2016-04-29", 
"2016-05-31", "2016-06-30", "2016-07-29", "2016-08-31", "2016-09-30", 
"2016-10-31", "2016-11-30", "2016-12-30", "2017-01-31", "2017-02-28", 
"2017-03-31", "2017-04-28", "2017-05-31", "2017-06-30", "2017-07-31", 
"2017-08-31", "2017-09-29", "2017-10-31", "2017-11-30", "2017-12-29", 
"2018-01-31", "2018-02-28", "2018-03-29", "2018-04-30", "2018-05-31", 
"2018-06-29", "2018-07-31", "2018-08-31", "2018-09-28", "2018-10-31", 
"2018-11-30", "2018-12-31"), class = "factor")), row.names = c(NA, 
100L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

您需要使用%m+%而不是+

mydates  %>% 
   mutate(date2 = rollback(parse_date(date, "%Y-%m-%d") %m+% months(1)))
#          date      date2
# 1  2014-01-31 2014-01-31
# 2  2014-02-28 2014-02-28
# 3  2014-03-31 2014-03-31
# 4  2014-04-30 2014-04-30
# 5  2014-05-30 2014-05-31
# 6  2014-06-30 2014-06-30
# 7  2014-07-31 2014-07-31
# 8  2014-08-29 2014-08-31
# 9  2014-09-30 2014-09-30
# 10 2014-10-31 2014-10-31

help('%m+%')
  

添加月份使基本算术失败,因为连续几个月   有不同的长度。与其他元素一起使用,对   算术执行自动翻转。例如12:00:00 + 61   秒变为12:01:01。但是,人们通常更喜欢   几个月不会发生这种行为。例如,有时我们想要一月   31 + 1个月= 2月28日,而不是3月3日。%m +%执行这种类型的   算术。日期%m +%months(n)始终返回第n个月的日期   日期之后。如果新日期通常会溢出到n + 1   月,%m +%将返回第n个月的最后一天(rollback())。   日期%m-%months(n)始终返回第n个月之前的日期   日期。