I've got some unusually formatted strings that I need to get into datetime objects, and I can't find what I need from the strptime function documentation. Examples of strings that I need formatted are:
4/16/2018 0:00:00
8/30/2019 14:35:00
11/15/2017 8:15:10
I can't find any specification that matches these kinds of strings that I can use as the format in strptime. Am I going to have to format the strings first?
We can use the format "%m/%d/%Y %H:%M:%S" to convert to DateTime
as.POSIXct(str1, format = "%m/%d/%Y %H:%M:%S")
#[1] "2018-04-16 00:00:00 CDT" "2019-08-30 14:35:00 CDT" "2017-11-15 08:15:10 CST"
data
str1 <- c("4/16/2018 0:00:00", "8/30/2019 14:35:00", "11/15/2017 8:15:10")
I think this is enough:
library(lubridate)
dates = c('4/16/2018 0:00:00', '8/30/2019 14:35:00', '11/15/2017 8:15:10')
dates = mdy_hms(dates)
dates
Output:
[1] "2018-04-16 00:00:00 UTC" "2019-08-30 14:35:00 UTC" "2017-11-15 08:15:10 UTC"
Seem to be quite common strings for strptime. Or was there another problem?
x <- "4/16/2018 0:00:01"
strptime(x ,"%m/%d/%Y %H:%M:%S")
Related
I would like to convert a string to time. I have a time field where the string has only four digits and a letter (A or P). There is no colon between the digits showing it is a time. I would like to convert the string, which is 12 hours, to a 24 hour time so I can drop the A and P.
Here is an example:
time = c("1110A", "1120P", "0420P", "0245P")
I'm looking for a time class that loos like this:
Answer= c('11:10', '23:20', '16:20', '14:45')
Any help would be greatly appreciated.
You can use the function strptime to create dates from strings after making one small change to your strings.
time <- c("1110A", "1120P", "0420P", "02:45P")
time <- gsub(":", "", time)
time <- strptime(x = paste0(time, "m"), format = "%I%M%p")
paste is needed for strptime to parse with the format that we've given it. %I is an hour (00-24), %M is the minute and %p is for parsing AM/PM.
Once it's parsed as a date, you can use format for pretty printing, or use the normal operators on it like +, -, diff, etc....
strptime gives you a lot of flexibility when parsing dates, but sometimes you have to try a few things when dates are not in a standard format.
We could also use the lubridate functions to parse the format after pasteing the date
library(lubridate)
library(glue)
ymd_hm(glue("2018-01-01 {time}M"))
#[1] "2018-01-01 11:10:00 UTC" "2018-01-01 23:20:00 UTC"
#[3] "2018-01-01 16:20:00 UTC" "2018-01-01 14:45:00 UTC"
In your question, you say that you want to be able to subtract these times. I think it makes the most sense to convert it to a POSIXct object. If you want a specific day/month/year you need to append it to your string like below, otherwise you can not specify one and it will assume the date is today:
date2 = as.POSIXct(paste0("01-01-2018 ", time, "m"), format = "%m-%d-%Y %I%M%p")
date2
#[1] "2018-01-01 11:10:00 EST" "2018-01-01 23:20:00 EST" "2018-01-01 16:20:00 EST" "2018-01-01 14:45:00 EST"
I have a dataset with a column where date and time is stored.
The data I have is:
03/17/2020 09:30:00 PM
I want to convert AM/PM to a 24hour format.
My attempt was using this:
as.POSIXct(df$Date, format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
When I run this with the whole dataset, the majority of dates turns into "NA".
Why is this happening? I am really confused.
Using lubridate:
x <- "03/17/2020 09:30:00 PM"
lubridate::mdy_hms(x)
[1] "2020-03-17 21:30:00 UTC"
Using as.Posixct: note that you need the month / day convention, not the day/month:
as.POSIXct(x, format="%m/%d/%Y %I:%M:%S %p", tz = "UTC")
[1] "2020-03-17 21:30:00 UTC"
How can I convert :
2019-01-22 18:30:33 UTC
to
2019-01-22 18:30
with delating ss and UTC word.
Thank you
Using base R:
Assuming your date is a string. Make it a POSIX object and format whichever way you want it.
date_as_posix <- strptime("2019-01-22 18:30:33 UTC", format="%Y-%m-%d %H:%M:%S", tz="UTC")
strftime(date_as_posix, format="%Y-%m-%d %H:%M", tz="UTC")
[1] "2019-01-22 18:30"
Best,
Chris
An option is floor_date assuming that the OP still want a Datetime object
library(lubridate)
floor_date(ymd_hms(str1), 'minute')
#[1] "2019-01-22 18:30:00 UTC"
If the intention is to get a string, then use format
format(ymd_hms(str1), "%Y-%m-%d %H:%M")
#[1] "2019-01-22 18:30"
data
str1 <- '2019-01-22 18:30:33 UTC'
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
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"