Convert the Character to Time in R [duplicate] - r

I have searched but I could not find out how to convert a date from a character string formatted as follows:
date <- "07-21-2015-09:30AM"
I wanted to use as.Date, but I have not manage to. All I get is the following:
as.Date(date, format="%m-%d-%y-%hAM")
NA
as.Date(dates, format="%m-%d-%y-%h")
NA

If we need the 'date' and 'time', one option is as.POSIXct
as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
#[1] "2015-07-21 09:30:00 EDT"

You can also use the lubridate package like this:
library('lubridate')
date <- "07-21-2015-09:30AM"
mdy_hm(date)
# "2015-07-21 09:30:00 UTC"

I like strptime for this:
strptime(date, format="%m-%d-%Y-%R%p")
#[1] "2015-07-21 09:30:00 EDT"
And in the case that you needed to see the date in the same format as entered, you can call the related strftime. It doesn't change the internal storage of the variable, rather it changes the format only.
strftime(xx, format="%m-%d-%Y-%R%p")
#[1] "07-21-2015-09:30AM"

Related

adding string at the end of a string for date time data format in R [duplicate]

I have searched but I could not find out how to convert a date from a character string formatted as follows:
date <- "07-21-2015-09:30AM"
I wanted to use as.Date, but I have not manage to. All I get is the following:
as.Date(date, format="%m-%d-%y-%hAM")
NA
as.Date(dates, format="%m-%d-%y-%h")
NA
If we need the 'date' and 'time', one option is as.POSIXct
as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
#[1] "2015-07-21 09:30:00 EDT"
You can also use the lubridate package like this:
library('lubridate')
date <- "07-21-2015-09:30AM"
mdy_hm(date)
# "2015-07-21 09:30:00 UTC"
I like strptime for this:
strptime(date, format="%m-%d-%Y-%R%p")
#[1] "2015-07-21 09:30:00 EDT"
And in the case that you needed to see the date in the same format as entered, you can call the related strftime. It doesn't change the internal storage of the variable, rather it changes the format only.
strftime(xx, format="%m-%d-%Y-%R%p")
#[1] "07-21-2015-09:30AM"

Two Timestamp Formats in R

Im have a time stamp column that I am converting into a POSIXct. The problem is that there are two different formats in the same column, so if I use the more common conversion the other gets converted into NA.
MC$Date
12/1/15 22:00
12/1/15 23:00
12/2/15
12/2/15 1:00
12/2/15 2:00
I use the following code to convert to a POSIXct:
MC$Date <- as.POSIXct(MC$Date, tz='MST', format = '%m/%d/%Y %H:%M')
The results:
MC$Date
15-12-01 22:00:00
15-12-01 23:00:00
NA
15-12-02 01:00:00
15-12-02 02:00:00
I have tried using a logic vector to identify the issue then correct it but can't find an easy solution.
The lubridate package was designed to deal with situations like this.
dt <- c(
"12/1/15 22:00",
"12/1/15 23:00",
"12/2/15",
"12/2/15 1:00",
"12/2/15 2:00"
)
dt
[1] "12/1/15 22:00" "12/1/15 23:00" "12/2/15" "12/2/15 1:00" "12/2/15 2:00"
lubridate::mdy_hm(dt, truncated = 2)
[1] "2015-12-01 22:00:00 UTC" "2015-12-01 23:00:00 UTC" "2015-12-02 00:00:00 UTC"
[4] "2015-12-02 01:00:00 UTC" "2015-12-02 02:00:00 UTC"
The truncated parameter indicates how many formats can be missing.
You may add the tz parameter to specify which time zone to parse the date with if UTC is not suitable.
I think the logic vector approach could work. Maybe in tandem with an temporary vector for holding the parsed dates without clobbering the unparsed ones. Something like this:
dates <- as.POSIXct(MC$Date, tz='MST', format = '%m/%d/%Y %H:%M')
dates[is.na(dates)] <- as.POSIXct(MC[is.na(dates),], tz='MST', format = '%m/%d/%Y')
MC$Date <- dates
Since all of your datetimes are separated with a space between date and time, you could use strsplit to extract only the date part.
extractDate <- function(x){ strsplit(x, split = " " )[[1]][1] }
MC$Date <- sapply( MC$Date, extractDate )
Then go ahead and convert any way you like, without worrying about the time part getting in the way.

Numeric to DateTime format in R

I would like to know what is the best way to convert 201509122150 (numeric) to Date class within YYYY-MM-DD hh:mm format.
E.g.
x <- 201509122150
as.POSIXct(as.character(x), format="%Y%m%d%H%M")
# [1] "2015-09-12 21:50:00 CEST"
This can be easily done with lubridate
library(lubridate)
ymd_hm(x)
#[1] "2015-09-12 21:50:00 UTC"
data
x <- 201509122150

How to convert a column with different date formats to a common date format?

Date
01/01/2013 #mm/dd/yyyy
12122015 #mmddyyyy
2014-03-21 #yyyy-mm-dd
95.10.12 #yy.mm.dd
I have a column "Date" with different formats. How can I clean this and convert them into a single date format?
Additional info:class(Date) is factor.
The easiest way to do this is to use the lubridate package.
Date=c(
"01/01/2013" #mm/dd/yyyy
,"12122015" #mmddyyyy
,"2014-03-21" #yyyy-mm-dd
,"95.10.12" #yy.mm.dd
)
library(lubridate)
# list of functions in lubridate that
# translate text to POSIXct: you only need to know the
# order of the data (e.g. mdy = month-day-year).
funs <- c("mdy","dym","ymd","dmy","myd","ydm")
# vector to store results
dates <- as.POSIXct(rep(NA,length(Date)))
# we try everything lubridate has. There will be some warnings
# e.g. because mdy cannot translate everything. You can ignore this.
for ( f in funs ){
dates[is.na(dates)] <- do.call(f,list(Date[is.na(dates)]))
}
dates
> dates
[1] "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET" "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET"

Converting non-standard dates in r

My date information is a string in the following format: 3/12/1956 0:00:00
I have tried converting it using DOB<-as.Date(DOB, "%d/%m/%y %H:%M:%S")
I am trying to convert it for the purpose of then applying the age_calc function in eeptools package.
Is there some other way to change a non standard format into a date. Damn Aussie dates!
You can try lubridate as an alternative
library(lubridate)
DOB <- '3/12/1956 0:00:00'
mdy_hms(DOB)
#[1] "1956-03-12 UTC"
It can also take multiple formats
DOB <- c('3/12/1956 0:00:00', '3.12/1956 0.00/00')
mdy_hms(DOB)
#[1] "1956-03-12 UTC" "1956-03-12 UTC"
Or as #Richard Scriven commented,
as.Date(DOB, "%d/%m/%Y %H:%M:%S")
#[1] "1956-12-03"

Resources