1

I have dates in a character vector and I'm looking for a way to transform it to date format with as.Date():

vector <- c("15/1/2019", "5/5/2019")

However, as.Date(vector, format="%d/%m/%Y") receives as input characters with zeros leading. And I have found plenty of solutions for:

Input: "15/01/2019"
Output: "15/1/2019"

However, I need exactly the opposite to make as.Date() to work:

Input: "15/1/2019" "5/5/2019"
Output: "15/01/2019" "05/05/2019"

Is there any specific formatting for format argument? Or I have to use a substr() solution?

neilfws
  • 32,751
  • 5
  • 50
  • 63
Chris
  • 2,019
  • 5
  • 22
  • 67
  • 1
    I've edited so as the dates in `vector` are quoted and match the output example. – neilfws May 14 '19 at 22:21
  • (also `vector` is an R function so not the greatest variable name) – neilfws May 14 '19 at 22:29
  • I'm so confused. What are the "plenty of solutions" to generate 15/1/2019 on output from an input of 15/01/2019? – thelatemail May 14 '19 at 22:29
  • It took me 5 seconds get these two: https://stackoverflow.com/questions/25387160/formatting-a-date-in-r-without-leading-zeros and https://stackoverflow.com/questions/27584346/no-leading-zeros-for-months-r – Chris May 15 '19 at 15:34
  • Why the downvote by the way? I can't find any solution for this out of this thread – Chris May 15 '19 at 15:36
  • I've edited my answer because it is clear from your comments that you are confusing the appearance of a Date object with the appearance of a formatted date string. – neilfws May 15 '19 at 22:21

2 Answers2

3

We can use strftime.

strftime(as.Date(inp, format="%d/%m/%Y"), format="%d/%m/%Y")
# [1] "15/01/2019" "05/05/2019"

Data

inp <- c("15/1/2019", "5/5/2019")
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • There's nothing magical: the`as.Date` reads character and converts into date, the `strftime` creates an output in desired format. The trick is to both specify correctly. See `?strftime`. – jay.sf May 15 '19 at 16:52
  • Thanks!. By the way, I had to add `%>% as.Date()` as the class of your result is character. – Chris May 15 '19 at 17:29
  • By your last comment I think you are confusing the appearance of a Date object with the appearance of a formatted date string. – neilfws May 15 '19 at 22:12
1

I'm not sure I understand the issue. as.Date() works as expected on your input:

vector <- c("15/1/2019", "5/5/2019")
dates <- as.Date(vector, format = "%d/%m/%Y")

dates
[1] "2019-01-15" "2019-05-05"

To get the desired output, you use format:

format(dates, "%d/%m/%Y")
[1] "15/01/2019" "05/05/2019"

EDIT

From your comments, it's clear that you are confusing two different things.

To get a Date object using as.Date(), you need to tell it the format of the input date character string. You have "day-month-year". So you supply that to as.Date():

as.Date(vector, format = "%d/%m/%Y")

The result will always be displayed in the console as year-month-day (YYYY-MM-DD).

Displaying the date in a chosen format is something different. Now we are formatting the Date object back to a character string:

format(dates, "%d/%m/%Y")

To summarise: a properly-converted Date object will always display in the console as YYYY-MM-DD. Formatting that object as a string is a different operation.

I hope this clears things up.

neilfws
  • 32,751
  • 5
  • 50
  • 63