[英]convert date in Month-Year format to last date of month
My dataset looks like this 我的数据集看起来像这样
dataset=data.frame(ID=c(1,2,3,4,5),MonthYear=c("May 2015","April 2015","January 2016","February 2016","December 2018"))
I'd like to add a column to it that contains the date of the last day of the month for the given month-year (column MonthYear
) 我想在其中添加一列 ,其中包含给定月份年份(每月MonthYear
) 的每月最后一天的日期。
For example, the month-year May 2015
would become 31-05-2015
例如,月年May 2015
将成为31-05-2015
Using the zoo
package I've tried to use as.Date(as.yearqtr(MonthYear, "%b%Y"), frac = 1)
based on a solution I found on this forum, but it doesn't seem to work. 根据我在此论坛上找到的解决方案as.Date(as.yearqtr(MonthYear, "%b%Y"), frac = 1)
使用了我尝试使用的as.Date(as.yearqtr(MonthYear, "%b%Y"), frac = 1)
的zoo
程序包,但它似乎不起作用。
With zoo
, instead of as.yearqtr
, we use as.yearmon
as the format is in 'Month Year' 使用zoo
而不是as.yearqtr
,我们使用as.yearmon
因为格式为“月年”
library(zoo)
as.Date(as.yearmon(dataset$MonthYear), frac = 1)
#[1] "2015-05-31" "2015-04-30" "2016-01-31" "2016-02-29" "2018-12-31"
The problem is that 问题是
your format is wrong. 您的格式错误。 The format shown in the question is for an abbreviated month followed by the year with no space between them. 问题中显示的格式是一个缩写的月份,后面是年份,并且两者之间没有空格。 In fact, the data has the full month name followed by a space followed by the year. 实际上,数据具有完整的月份名称,后跟一个空格,然后是年份。
yearqtr
is used in the code in the question but that is for year and quarter of a year whereas you have year and month of year. 在问题的代码中使用yearqtr
,但这是一年和一年的四分之一,而您有一年的年和月。 Use yearmon
, not yearqtr
. 使用yearmon
而不是yearqtr
。
Making these changes yields the following code 进行这些更改将产生以下代码
transform(dataset, eom = as.Date(as.yearmon(MonthYear, "%B %Y"), frac = 1))
giving: 给予:
ID MonthYear eom
1 1 May 2015 2015-05-31
2 2 April 2015 2015-04-30
3 3 January 2016 2016-01-31
4 4 February 2016 2016-02-29
5 5 December 2018 2018-12-31
Using lubridate
we can convert MonthYear
to date object and use ceiling_date
with unit = "Month"
and subtract 1 day from it to get last day of the month. 使用lubridate
我们可以将MonthYear
转换为date对象,并使用ceiling_date
和unit = "Month"
并从中减去1天以得出该月的最后一天。
library(lubridate)
ceiling_date(dmy(paste("01", dataset$MonthYear)), unit = "month") - 1
#[1] "2015-05-31" "2015-04-30" "2016-01-31" "2016-02-29" "2018-12-31"
In base R we may add a month with seq
and subtract a day. 在基数R中,我们可以将seq
加一个月,再减去一天。
as.Date(mapply(function(x) seq(x, length.out=2, by="month")[2] - 1,
as.Date(paste("01", dataset$MonthYear), "%d %B %Y")),
origin="1970-01-01")
# [1] "2015-05-31" "2015-04-30" "2016-01-31" "2016-02-29" "2018-12-31"
Note: as.Date(dataset$MonthYear, "%B %Y")
won't work somehow, I don't know why...? 注意: as.Date(dataset$MonthYear, "%B %Y")
将无法正常工作,我不知道为什么...?
Data 数据
dataset <- structure(list(MonthYear = structure(c(5L, 1L, 4L, 3L, 2L), .Label = c("April 2015",
"December 2018", "February 2016", "January 2016", "May 2015"), class = "factor")), class = "data.frame", row.names = c(NA,
-5L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.