lapioche75 lapioche75 - 2 months ago 18
R Question

Dates format in R: 3/31/2016 io 03/31/2016

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

Answer

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.