Remove leading 0 from date converted to character

Let do:
> as.character(as.Date("2020-06-07"), format="%d/%m/%y")
[1] "07/06/20"

But what to do if you want just 7/6/20 ?

Here is the solution:

> gsub("^0", "", gsub("([-])0", "\\1", as.character(as.Date("2020-06-07"), format="%d/%m/%y")))
[1] "7/06/20"

Or

> d <- as.character(as.Date("2020-06-07"), format="%d/%m/%y")
> gsub("^0", "", gsub("/0", "/", d))
[1] "7/6/20"

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04