0

The example with some dates and transformation into date

library(lubridate)
date <- c('2018-01-09', '2018-01-10')
date = ymd(date)

I'm trying to extract month and day from the dates and keep them in one column of date-class:

date <- format(date, "%m-%d")

the class is changed from date to chr, but I want to keep the date class. What should I do?

  • 1
    Afaik you can't do that; date class has to have a year. – jtr13 Apr 07 '23 at 14:10
  • 2
    This is not possible. See here comment of @Ronak Shah : "No, date has fixed format. (YYYY-MM-DD) everything else is just character. If you are using ggplot for plotting I would suggest to keep the dates as it is and use functions like scale_x_date or scale_x_datetime to format labels as per your choice." – TarJae Apr 07 '23 at 14:10

1 Answers1

0

The Date class in R represents an object as the number of days since 1970/01/01. This cannot be done for a month and day with no year.

You might consider an alternative representation that captures both month and day in one vector: day of year lubridate::yday(date)

cmcrowley
  • 95
  • 6