-1

Sorry about another date-question, but I couldn't find an answer. I have a date-type "September 2017" and need to convert it to "30.09.2017" (last day of month — for all months of all years). I've tried:

date <- as.Date(date), "%B %d %Y")

+ variations of %d%m%y

And that:

library(zoo)
date <- as.Date(as.yearmon(date))

But every time I have NA.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Use `as.Date(paste(30, "September 2017"), format="%d %B %Y")`. You need to include a day. Month Year is not sufficient. – lmo Oct 09 '17 at 16:36
  • 1
    Mind your [locales when using `%B`](https://stackoverflow.com/questions/13726894/strptime-as-posixct-and-as-date-return-unexpected-na). – Henrik Oct 09 '17 at 16:38
  • `> as.Date(paste(01, "September 2017"), format="%d %B %Y") [1] NA` – Ekaterina Kolmakova Oct 09 '17 at 16:51

1 Answers1

2

This works for me:

> library(zoo)
> s = "September 2017"
> as.yearmon(s)
[1] "Sep 2017"

If I then convert to date, it gets the first of the month:

> as.Date(as.yearmon(s))
[1] "2017-09-01"

This seems to be exactly what you are doing but you don't explicitly show us where your "September 2017" string is so I suspect a problem there...

As for the "30th", what are you going to do about February? You can use frac=1 to get the last day of the month (hat tip @Henrik):

> as.Date(as.yearmon(s),frac=1)
[1] "2017-09-30"
> as.Date(as.yearmon("February 2018"),frac=1)
[1] "2018-02-28"
> as.Date(as.yearmon("February 2020"),frac=1)
[1] "2020-02-29"
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Great question about February, I'll keep 01.09.. I have a file name "X Y Z W September, 2017". Than I did that: `date <-append(unlist(strsplit(file.list[i], ' '))[5], unlist(strsplit(file.list[i], ' '))[6]) year <- strsplit(date[2], '.', fixed=TRUE)[[1]][1] date <- sub(',', '', date) paste(date[1], year, collapse =' ')` And then: `date <- as.Date(as.character(paste(date[1], year, collapse =' ')), "%B %d %Y")` or `date <- as.Date(as.yearmon(paste(date[1], year, collapse =' ')))` – Ekaterina Kolmakova Oct 09 '17 at 16:42
  • See the `frac` argument of `as.Date.yearmon` – Henrik Oct 09 '17 at 16:43
  • I'm so sorry about Enter's, don't know why they're not working, trying to fix. – Ekaterina Kolmakova Oct 09 '17 at 16:46
  • One way to get to the last day of the current month is to find the first day of the next month and subtract one day from that. How easy that is depends on the functions in the library, and there I'm not an expert for R. The first day of the current month is usually relatively simple; maybe adding one month to that is easy, and then subtracting one day. – Jonathan Leffler Oct 09 '17 at 16:53
  • Edit to show how to get the last day, thanks @Henrik – Spacedman Oct 09 '17 at 16:54
  • Last day is not principle, it can be the first day, that's not a problem. But thank you! – Ekaterina Kolmakova Oct 09 '17 at 16:56
  • `> l <- paste(date[1], year, collapse =' ') > as.yearmon(l) [1] NA > as.Date(as.yearmon(l),frac=1) [1] NA` – Ekaterina Kolmakova Oct 09 '17 at 17:01