Home > other >  How to fix character dates and add zeros to it in R?
How to fix character dates and add zeros to it in R?

Time:03-03

I have a dataset that all of it’s date variables are messed up. All of the columns are characters. They look like this:

name <- c(“Ana”, “Maria”, “Rachel”, “Julia”)
date_of_birth <- c(“9/8/1997”, “22/3/1966”, “24/10/1969”, “25/6/2019”)

data <- as.data.frame(cbind(name, date_of_bieth))

I need to turn those dates into dd/mm/yyyy format. They are already in this order, but I need to add zero when dd or mm has only one digit.

For example, “9/8/1997” should be “09/08/1997”.

CodePudding user response:

We can try this

> format(as.Date(date_of_birth, format = "%d/%m/%Y"), "%d/%m/%Y")
[1] "09/08/1997" "22/03/1966" "24/10/1969" "25/06/2019"
  • Related