简体   繁体   English

R中一系列日期中最后一次观察月份的顺序

[英]Sequence of last observation of months in a series of dates in R

If I have a range of business day dates from 2010-01-04 to 2014-12-31 , how do I create a vector of dates containing the last date for each month in my data? 如果我有一个范围为2010-01-042014-12-31工作日日期,如何创建包含数据中每个月的最后日期的日期向量?

I've looked through zoo and lubridate packages but couldn't find anything relevant. 我查看了动物园和润滑包装,但找不到任何相关的内容。

Here's a two-step process to get the job done: 这是完成工作的两步过程:

  1. Use lubridate::ceiling_date to get the first day of next month 使用lubridate::ceiling_date获取下个月的第一天
  2. We generate a sequence "by month" and subtract 1 day 我们生成一个“按月”序列并减去1天

# generage some random dates
set.seed(123)
mydates <- sample(seq(from = as.Date("2017-01-01"), 
                      to   = as.Date("2018-12-31"), by="day"), 10)

# generate requested sequence
d1 <- lubridate::ceiling_date(min(mydates), "month")
d2 <- lubridate::ceiling_date(max(mydates), "month")

result <- seq(from=d1, to=d2, by="month") - 1

It's not clear whether you want all end of months for the two dates plus all months between them or just for the two dates but below we show both. 目前尚不清楚您是要两个日期的所有月末加上它们之间的所有月份,还是只想两个日期,但是下面我们都显示了两者。

1) Here is a one-liner using the yearmon class and gives all end of months for the from the first date to the second date. 1)这是使用yearmon类的yearmon并给出了从第一个日期到第二个日期的所有月末。 Convert each input character string to yearmon class, create a sequence of them and convert to Date class using frac equals 1 (which means last day of month). 将每个输入的字符串转换为yearmon类,创建它们的序列,然后使用等于1(表示每月的最后一天)的frac转换为Date类。

library(zoo)
as.Date(seq(as.yearmon("2010-01-04"), as.yearmon("2014-12-31"), 1/12), frac = 1)

giving: 赠送:

[1] "2010-01-31" "2010-02-28" "2010-03-31" "2010-04-30" "2010-05-31"
[6] "2010-06-30" "2010-07-31" "2010-08-31" "2010-09-30" "2010-10-31"
[11] "2010-11-30" "2010-12-31" "2011-01-31" "2011-02-28" "2011-03-31"
[16] "2011-04-30" "2011-05-31" "2011-06-30" "2011-07-31" "2011-08-31"
[21] "2011-09-30" "2011-10-31" "2011-11-30" "2011-12-31" "2012-01-31"
[26] "2012-02-29" "2012-03-31" "2012-04-30" "2012-05-31" "2012-06-30"
[31] "2012-07-31" "2012-08-31" "2012-09-30" "2012-10-31" "2012-11-30"
[36] "2012-12-31" "2013-01-31" "2013-02-28" "2013-03-31" "2013-04-30"
[41] "2013-05-31" "2013-06-30" "2013-07-31" "2013-08-31" "2013-09-30"
[46] "2013-10-31" "2013-11-30" "2013-12-31" "2014-01-31" "2014-02-28"
[51] "2014-03-31" "2014-04-30" "2014-05-31" "2014-06-30" "2014-07-31"
[56] "2014-08-31" "2014-09-30" "2014-10-31" "2014-11-30" "2014-12-31"

2) or if you mean you just want the corresponding end of month for each input date then here is a one-liner for that: 2)或如果您只是想为每个输入日期指定对应的月末,则这里是一线的:

library(zoo)

x <- c("2010-01-04", "2014-12-31") # input data
as.Date(as.yearmon(x), frac = 1)

giving: 赠送:

[1] "2010-01-31" "2014-12-31"

2a) To do this without any packages first define a first of month function and then apply it twice as shown. 2a)要在没有任何软件包的情况下执行此操作,请首先定义“月初”功能,然后如图所示应用两次。

fom <- function(x) as.Date(cut(as.Date(x), "month"))
fom(fom(x) + 31) - 1

giving: 赠送:

[1] "2010-01-31" "2014-12-31"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM