Convert string "DDMMMYYYY:HH:MM:SS" to date format - r

I have imported a sas file with date ion the format 21JAN1988:00:00:00 seen as factor in R.
I want to convert this into an r date format 1988-01-21.
How do I go about this?

Using as.Date:
x <- "21JAN1988:00:00:00"
as.Date(x, format = "%d%b%Y")
# [1] "1988-01-21"
Using anytime package:
anytime::anydate(x)
# [1] "1988-01-21"
anytime::anytime(x)
# [1] "1988-01-21 01:00:00 GMT"
Note: There must be a duplicate for this post, but I couldn't find. Let me know in the comments if you find one, I will convert this post to community wiki.

Related

Converting time/date format character string to Date in R [duplicate]

A client sent me an Excel file with dates formatted as e.g 3/15/2012 for March 15. I saved this as a .csv file and then used
camm$Date <- as.Date(camm$Date, "%m/%d/%y")
but this gave me values starting in the year 2020!
I tried to reformat the dates in the original csv file so that they were e.g. 03/14/2013 but was unable to do so.
Any help appreciated
Use capital Y in as.Date call instead. This should do the trick:
> as.Date("3/15/2012", "%m/%d/%Y")
[1] "2012-03-15"
From the help file's examples you can realize when year is full specified you should use %Y otherwise %y for example:
> dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
> as.Date(dates, "%m/%d/%y")
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"
You can see that in your example the Year format is 2012 then you should use %Y, and in the other example (taken from the as.Date help file) Year format is 92 then using %y is the correct way to go. See as.Date for further details.
You might also give a try to the lubridate package if you do not want to deal with the hieroglyphics :)
> library(lubridate)
> parse_date_time('3/15/2012', 'mdy')
1 parsed with %m/%d/%Y
[1] "2012-03-15 UTC"
PS.: of course I do not encourage anyone to use any extra dependencies, this answer was just posted here as an alternative (and quick to remeber) solution
To complete the picture, you might also try the recently introduced (2016-09) package anytime which takes advantage of the Boost C++ libraries:
anytime::anytime("3/15/2012")
#[1] "2012-03-15 CET"
We can use mdy from lubridate
lubridate::mdy('3/15/2012')
#[1] "2012-03-15"
Or parse_date from readr which uses same format as as.Date
readr::parse_date('3/15/2012', '%m/%d/%Y')
#[1] "2012-03-15"

Converting Date Columns in R ("character" into Date format)

I have a sample date column as part of large data set, Below date is in multiple format.
I need convert below mentioned into Date format , Please help me with a solution.
22-04-2015
4/8/2015
18-04-2015
5/7/2015
26-05-2015
6/12/2015
24-06-2015
23-06-2015
Try with lubridate. The function guess_formats() allows defining possible formats of your data (you could add others if needed), and then you can use as.Date() to get the dates in the proper class using the formats previously defined. Here the code:
library(lubridate)
#Dates
vecdate <- c('22-04-2015', '4/8/2015','18-04-2015','5/7/2015','26-05-2015',
'6/12/2015','24-06-2015','23-06-2015')
#Formats
formats <- guess_formats(vecdate, c("dmY"))
dates <- as.Date(vecdate, format=formats)
Output:
dates
[1] "2015-04-22" "2015-08-04" "2015-04-18" "2015-07-05" "2015-05-26" "2015-12-06" "2015-06-24"
[8] "2015-06-23" "2015-04-22" "2015-08-04" "2015-04-18" "2015-07-05" "2015-05-26" "2015-12-06"
[15] "2015-06-24" "2015-06-23"
Have you tried to use R package anytime for date conversion?
> library(anytime)
> datemix <- c("22-04-2015", "4/8/2015", "18-04-2015", "5/7/2015")
> anydate(datemix)
[1] NA "2015-04-08" NA "2015-05-07"
You notice that the first and third character dates came up with NA. This is because the package anytime as currently defined does not include this format "d-m-y". It is very easy to add this format with the addFormats() command as shown below
> addFormats("%d-%m-%Y")
> datemix <- c("22-04-2015", "4/8/2015", "18-04-2015", "5/7/2015")
> anydate(datemix)
[1] "2015-04-22" "2015-04-08" "2015-04-18" "2015-05-07"
The output dates are converted into ISO format of YYYY-MM-DD.
You can explore all the formats in anytime using getFormats() command. Here is the link to the anytime on R CRAN https://cran.r-project.org/web/packages/anytime/anytime.pdf

How to convert a datetime to date in R, WITHOUT rounding the day?

I'm using R and trying to convert a datetime field into just the date? R gives me the desired format but keeps rounding up some of the day values. Specifically everything after 12 noon! I could not find any threads that address this exact problem. I actually figured out a solution but wanted to post the question because I spent a whole week troubleshooting.
#Convert the datetime field from character to a datetime
main_df$datetime <- strptime(main_df$ï..Date, format = "%m/%d/%Y %H:%M")
main_df$datetime <- as.POSIXct(main_df$datetime, tz = Sys.timezone())
head(main_df$datetime)
class(main_df$datetime)
#Remove the poorly computer-titled character field that contained datetime info
main_df <- subset(main_df, select = -c(ï..Date))
#Use the NEW datetime field to create a date field
#main_df$Date <- trunc(main_df$datetime,"days")
main_df$Date <- as.Date(main_df$datetime, format = "%m/%d/%Y")
?as.Date()
class(main_df$Date)
head(main_df$Date)
That returned:
head(main_df$datetime)
[1] "2020-05-16 00:31:00 CDT" "2020-05-16 00:30:00 CDT" "2020-05-15 23:33:00 CDT" "2020-05-15 15:33:00 CDT"
[5] "2020-05-15 22:31:00 CDT" "2020-05-15 22:12:00 CDT"
and
> class(main_df$Date)
[1] "Date"
>
> head(main_df$Date)
[1] "2020-05-16" "2020-05-16" "2020-05-16" "2020-05-15" "2020-05-16" "2020-05-16"
Notice how the last 4 values for 'Date' should be 2020-05-15 but instead, they are converted to be 2020-05-16. So what are some other ways to fix this? I'm going to post one way that worked but I doubt it's the cleanest.
If we don't want to make use of the 'time', then use a regex to match a space followed by other characters (" .*"), replace with blank in sub and then convert to Date class. The issue with converting to DateTime is that there are times "23:33:00" that would make it convert to next day
main_df$Date <- as.Date(sub(" .*", "", main_df$datetime), format = "%m/%d/%Y")
Do your input dates include a timezone specification? If not, they are ambiguous and the rounding may be right or it may be wrong. If they do include a timezone specification, the lubridate package should handle them correctly.
I would advise against using tz = Sys.timezone() because that would make the interaction between input data and algorithm dependent on geography if your inputs don't include a timezone specification, so what works for you might not work for a different user in a different location.
You could just extract date from datetime as a substring and convert it to Date type, using substr(x,begin,end) function, where x - your column, begin and end - begin and end of string to extract.
main_df$Date <- as.Date(substr(main_df$datetime,1,10))
I had the same issue and that function helped me to convert Datetime to Date without rounding.

From number to formatted datetime in R

I have a field into a dataframe of class numeric.
I want to convert that into a date time format.
value: 1353959527000000
expected: 2012-11-26 11:52:07.000-08:00
How do I do that in R?
I tried:
Using lubridate or default Posix conversion and nothing produced the date above. Read a bunch of posts and still not figuring out what I am doing wrong.
dn <- 1353959527000000
as.POSIXct(as.numeric(as.character(dn)),origin="1970-01-01 00:00:00")
output was something super off the expected date with some gibberish.
Same output trying this
as_datetime(1353959527000000, origin = "1970-01-01 00:00:00")
It's FAQ and a repeat question, but as #r2evans told you, the right scale helps. As does eg anytime::anytime as it frees you from using the origin etc pp:
R> dn <- 1353959527000000
R> anytime::anytime(dn/1e6) # local time
[1] "2012-11-26 13:52:07 CST"
R> anytime::utctime(dn/1e6) # utctime
[1] "2012-11-26 19:52:07 UTC"
R>

as.Date with dates in format m/d/y in R

A client sent me an Excel file with dates formatted as e.g 3/15/2012 for March 15. I saved this as a .csv file and then used
camm$Date <- as.Date(camm$Date, "%m/%d/%y")
but this gave me values starting in the year 2020!
I tried to reformat the dates in the original csv file so that they were e.g. 03/14/2013 but was unable to do so.
Any help appreciated
Use capital Y in as.Date call instead. This should do the trick:
> as.Date("3/15/2012", "%m/%d/%Y")
[1] "2012-03-15"
From the help file's examples you can realize when year is full specified you should use %Y otherwise %y for example:
> dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
> as.Date(dates, "%m/%d/%y")
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"
You can see that in your example the Year format is 2012 then you should use %Y, and in the other example (taken from the as.Date help file) Year format is 92 then using %y is the correct way to go. See as.Date for further details.
You might also give a try to the lubridate package if you do not want to deal with the hieroglyphics :)
> library(lubridate)
> parse_date_time('3/15/2012', 'mdy')
1 parsed with %m/%d/%Y
[1] "2012-03-15 UTC"
PS.: of course I do not encourage anyone to use any extra dependencies, this answer was just posted here as an alternative (and quick to remeber) solution
To complete the picture, you might also try the recently introduced (2016-09) package anytime which takes advantage of the Boost C++ libraries:
anytime::anytime("3/15/2012")
#[1] "2012-03-15 CET"
We can use mdy from lubridate
lubridate::mdy('3/15/2012')
#[1] "2012-03-15"
Or parse_date from readr which uses same format as as.Date
readr::parse_date('3/15/2012', '%m/%d/%Y')
#[1] "2012-03-15"

Resources