10

I would like to create a vector of dates between two specified moments in time with step 1 month, as described in this thread (Create a Vector of All Days Between Two Dates), to be then converted into factors for data visualization.

However, I'd like to have the dates in the YYYY-Mon, ie. 2010-Feb, format. But so far I managed only to have the dates in the standard format 2010-02-01, using a code like this:

require(lubridate)
first <- ymd_hms("2010-02-07 15:00:00 UTC")
start <- ymd(floor_date(first, unit="month"))

last <- ymd_hms("2017-10-29 20:00:00 UTC")
end <- ymd(ceiling_date(last, unit="month"))

> start
[1] "2010-02-01"

> end
[1] "2017-11-01"

How can I change the format to YYYY-Mon?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Fabio Capezzuoli
  • 599
  • 7
  • 23

1 Answers1

10

You can use format():

start %>% format('%Y-%b')

To create the vector, use seq():

seq(start, end, by = 'month') %>% format('%Y-%b')

Obs: Use capital 'B' for full month name: '%Y-%B'.

Augusto Fadel
  • 199
  • 1
  • 6