简体   繁体   中英

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 )

For example, the month-year May 2015 would become 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.

With zoo , instead of as.yearqtr , we use as.yearmon as the format is in 'Month Year'

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

  1. 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.

  2. 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. Use yearmon , not 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.

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.

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...?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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