How to Convert Dd/Mm/Yy to Yyyy-Mm-Dd in R

How to convert dd/mm/yy to yyyy-mm-dd in R

Use lubridate package

library(lubridate)
dmy("27/06/16")

Change date from dd-MM-YYYY to yyyy-mm-dd

This should make it possible to change your date to an appropriate date format for R

dates <- c("03-Apr-95", "04-Apr-95")

newFormat <- as.Date(dates, tryFormats = c("%d-%b-%y"))

[1] "1995-04-03" "1995-04-04"

Then format it in the usual way

format(newFormat, "%d-%m-%Y")

[1] "03-04-1995" "04-04-1995"

convert orderdate of format m/d/yy to YYYY-MM-DD in R

If you really have values like "1/1/2017 1:05:00 AM" then those aren't dates, they are date times, and as such you have to specify formatting characters for both the date and time parts.

So, first you need to get your date times into a form R understands as such (e.g. POSIXct) by specifying all the parts of the date time:

test <- as.POSIXct("1/1/2017 1:05:00 AM", format = '%m/%d/%Y %I:%M:%S %p')
test

> test
[1] "2017-01-01 01:05:00 CST"

See ?strftime if you are not familiar with all the formatting placeholders used above, and note the conditions for use of %I and %p.

Then you can convert the POSIXct vector into the date format you desire

format(test, format = '%Y-%m-%d')

> format(test, format = '%Y-%m-%d')
[1] "2017-01-01"

A complication for you is that R has converted your character date times into a factor, so you need to convert them back to a character vector before converting to date times. For example (not tested as you didn't supply example data)

orders2017 <- transform(orders2017,
orderdate = as.POSIXct(as.character(orderdate),
format = '%m/%d/%Y %I:%M:%S %p'))
orders2017 <- transform(orders2017,
newdate = format(orderdate, format = '%Y-%m-%d'))

How do I change the format of all dates in my dataframe from dd-mm-yyyy to yyyy-mm-dd (in r)

Do this in 2 steps. First, use as.Date to convert your text dates to bona fide R dates. Then use format to convert back to text, in the format you want.

dates <- as.Date(df$Date, format="%d-%m-%Y")
df$Date <- format(dates, "%Y-%m-%d")

Convert dd/mm/yy and dd/mm/yyyy to Dates

You can use parse_date_time from lubridate:

some.dates <- c("23/11/12", "20/10/2012", "22/10/2012" ,"23/11/12")
parse_date_time(some.dates,c('dmy'))
[1] "2012-11-23 UTC" "2012-10-20 UTC" "2012-10-22 UTC" "2012-11-23 UTC"

But , Note that the order of format is important :

some.dates <- c("20/10/2012","23/11/12",  "22/10/2012" ,"23/11/12")
parse_date_time(some.dates,c('dmY','dmy'))

[1] "2012-10-20 UTC" "2012-11-23 UTC" "2012-10-22 UTC" "2012-11-23 UTC"

EDIT

Internally parse_date_time is using guess_formats (which I guess uses some regular expressions):

guess_formats(some.dates,c('dmy'))
dmy dmy dmy dmy
"%d/%m/%Y" "%d/%m/%y" "%d/%m/%Y" "%d/%m/%y"

As mentioned in the comment you can use parse_date_time like this:

as.Date(dates, format = guess_formats(dates,c('dmy')))

How to convert date from mm/dd/YYYY to dd/mm/YYYY in R?

Don't use regex or string manipulation for date-time operations.

Convert to standard date class and then use format to get data in desired format.

format(as.Date('3/28/2020', '%m/%d/%Y'), '%d/%m/%Y')
#[1] "28/03/2020"

R - Date ddmmyyyy and dd/mm/yyyy in same column

We can use parse_date_time from lubridate.

date = c("22102002", "23122002", "23/12/02", "29/12/03", ""   ,"04/06/04") 
as.Date(lubridate::parse_date_time(date, c('dmY', 'dmy')))
#[1] "2002-10-22" "2002-12-23" "2002-12-23" "2003-12-29" NA "2004-06-04"

How to convert full date character into mm/dd/yyyy in R?

We need to convert it to Date class and then use format

format(as.Date(dates, "%B %d %Y"), "%m/%d/%Y")
#[1] "08/24/2012"

As the order is month, day, year use mdy from lubridate

library(lubridate)
format(mdy(dates), "%m/%d/%Y")
#[1] "08/24/2012"

It is based on the order, e.g.

ydm("2012 24 August")
#[1] "2012-08-24"

data

dates <- "August 24 2012"


Related Topics



Leave a reply



Submit