1

Is there a way to have the dates in R as 3/31/2016 (not showing 03 for the month)?

Right now my code is:

dates <- seq(as.Date("2000-1-1"), as.Date("2016-10-1"), by="3 months") -1
dates <- format(dates, format = "%m/%d/%Y")

But the outcome is

[1] "12/31/1999" "03/31/2000" "06/30/2000" "09/30/2000"

Thank you

lapioche75
  • 87
  • 1
  • 2
  • 9

1 Answers1

0

We can use sub, match the pattern of '0' at the start (^) of the string and replace it with blanks ("").

dates1 <- sub("^0", "", dates)
head(dates1)
#[1] "12/31/1999" "3/31/2000"  "6/30/2000"  "9/30/2000"  "12/31/2000" "3/31/2001" 

NOTE: It is not clear why the standard format is not needed.

akrun
  • 874,273
  • 37
  • 540
  • 662
  • great one more time! I need this new format to match a format from a csv file I'm importing and don't want to overwrite. – lapioche75 Oct 04 '16 at 13:43
  • That won't create single digit days. Try eg March 2nd. – Dirk Eddelbuettel Oct 04 '16 at 14:08
  • @DirkEddelbuettel I thought about the days, but it seems like the OP specified about the months. If we need the single digit days as well `sub("^0(.{2})0?", "\\1", c(dates[1:3], "03/02/2000")) #[1] "12/31/1999" "3/31/2000" "6/30/2000" "3/2/2000"` – akrun Oct 04 '16 at 14:47