2

I am trying to convert a vector of factors into a vector of dates. The data is formatted as month/date (e.g. 5/20, 4/13, 11/11). I want to retain the format but need to change the data type from factor to date.

df$date <- as.Date(df$date, format = '%m/%d')

Is what I have at the moment. What is returned is 2017-5-20 or 2017-4-13 for example. My question is, is there a way to have as.Date not return the year? Additionally, is there a way to do this in lubridate that may be more efficient? I would like my output to return 5-20 or 4-13. All examples I find online always include the year and seem to leave my question unanswered.

DPek
  • 180
  • 2
  • 15
  • 4
    It isn't a valid date without a year. If your concern is to have just month/day for _display_ purposes then I would handle that at the point when you're displaying the values. For instance, you could convert to character and extract a substring, or if you are plotting in ggplot you can control how the date is formatted in the plot. – joran Sep 20 '17 at 15:49
  • 1
    I'm not sure, but I think all date classes require the year. You can create your own class, though, or use it as a character vector. It depends on the operations you want to do with it. – csgroen Sep 20 '17 at 15:49
  • Thank you! I am processing a large data base for someone else to use for their own purposes but this information is helpful to know. I will have to consult with them to see how they would like this issue to be handled. – DPek Sep 20 '17 at 15:55

0 Answers0