Parse date using lubridate in R - r

I want to parse date format like "Apr-13", "May-14", which I want to parse it to POSIX time "2013-04-01 UTC", "2014-05-01 UTC". I tried using lubridate package in R:
parse_date_time("Apr-13", "my")
parse_date_time("Apr-13", "by")
parse_date_time("Apr-13", "%b-%y")
parse_date_time("Apr-13", "%m-%y")
fast_strptime("Apr-13", "%b-%y")
But all of them failed to parse, is it possible to parse this using lubridate? Any other package is also welcome.

Try this example:
library(lubridate)
x <- c("Apr-13", "May-14")
#add DD as 01
x <- paste("01",x,sep="-")
#result
dmy(x)
#output
#[1] "2013-04-01 UTC" "2014-05-01 UTC"

Related

Extract time (HMS) from lubridate date time object?

I have the following datetime:
t <- "2018-05-01 23:02:50 UTC"
I want to split it to time and date.
When I apply date(t) I get the date part.
But when I use lubridate's hms, parse_date_time and other functions to do this in "HMS" order I get NA.
I have checked other answers here on SOF but for some reason it gives me NA.
Please advise how to extract it.
I want to understand why:
strftime(t, format="%H:%M:%S")
will do the job but what I am missing in lubridate::hms or parse_date_time?
Is this what you were looking for? It can now be done more simply with hms::as_hms.
> library(lubridate)
> library(hms)
> as_hms(ymd_hms("2018-05-01 23:02:50 UTC"))
23:02:50
> t <- "2018-05-01 23:02:50 UTC"
> as_hms(ymd_hms(t))
23:02:50
My solution is to install library(anytime):
date <- anytime::anydate(t)
time <- strftime(t, format="%H:%M:%S")
What you are missing in lubridate's hms() is that it expects "a character vector of hour minute second triples" as an argument. There's no provision for handling a string which also contains date info. Hence, the output of Sys.Date() or lubridate::now() doesn't work as input to lubridate::hms().
In case you want a tidyverse solution, here's one:
library(tidyverse)
library(lubridate)
now()
#> [1] "2018-08-13 16:41:31 BST"
get_time <- function(time = now()) {
time %>%
str_split(" ") %>%
map_chr(2) %>%
hms()
}
get_time()
#> [1] "16H 41M 31S"
get_time("2018-05-01 23:02:50 UTC")
#> [1] "23H 2M 50S"
Created on 2018-08-13 by the reprex package (v0.2.0).
Something like this?
library(hms)
t <- "2018-05-01 23:02:50 UTC"
unlist(strsplit(t," "))[2]%>%hms::parse_hms()
Here is a solution without including just another package (hms on top of lubridate):
t <- "2018-05-01 23:02:50 UTC"
sprintf("%02d:%02d:%02d", hour(t), minute(t), second(t))
"23:02:50"
The fucntion in Lubridate package exists, it is called "hour()", here the official guide:
https://lubridate.tidyverse.org/reference/hour
t <- "2018-05-01 23:02:50 UTC"
lubridate::hour(t)
Posting a solution that I have used to extract either ymd or the hms independently after a lubridate conversion. I noticed OP mentioned wanting to separate both time and date so for future SO users who may find themselves here I included the regex's used for both below. assuming df contains a column called date where time is formated ymd_hms like so "2018-05-01 23:02:50 UTC":
library(stringr)
df <- df %>%
mutate(
time = str_extract(date, "[0-9]{2}:[0-9]{2}:[0-9]{2}") #to extract hms time
day = str_extract(date, "[0-9]{4}-[0-9]{2}-[0-9]{2}") #to extract ymd time
)

Convert the Character to Time 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"

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

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"

Parse timestamp with a.m./p.m

I have a file that formats time stamps like 25/03/2011 9:15:00 p.m.
How can I parse this text to a Date-Time class with either strptime or as.POSIXct?
Here is what almost works:
> as.POSIXct("25/03/2011 9:15:00", format="%d/%m/%Y %I:%M:%S", tz="UTC")
[1] "2011-03-25 09:15:00 UTC"
Here is what is not working, but I'd like to have working:
> as.POSIXct("25/03/2011 9:15:00 p.m.", format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
[1] NA
I'm using R version 2.13.2 (2011-09-30) on MS Windows. My working locale is "C":
Sys.setlocale("LC_TIME", "C")
It appears the AM/PM indicator can't include punctuation. Try it after removing the punctuation:
td <- "25/03/2011 9:15:00 p.m."
tdClean <- gsub("(.)\\.?[Mm]\\.?","\\1m",td)
as.POSIXct(tdClean, format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
# [1] "2011-03-25 21:15:00 UTC"
Just came across this, as another option you can use stringr package.
library(stringr)
data$date2 <- str_sub(data$date, end = -4)
# this removes the punctuation but holds onto the A/P values
data$date2 <- str_c(data$date2, 'm')
# adds the required m

Resources