String column to Dtm(date time) column in R - r

How does one convert a column from str to dtm? I've tried as.date and strptime and non of those works. Say I have a table with a column with 3 attributes (2003/11/04 19:29, 2001/04/02 21:32, 2003/10/28 09:51) in the str format. How would I covert this column so that it is in the dtm format? Thank you in advance!

Check ?strptime for different format arguments. You can do:
x <- c('2003/11/04 19:29', '2001/04/02 21:32', '2003/10/28 09:51')
as.POSIXct(x, format = "%Y/%m/%d %H:%M", tz = "UTC")
#Can also be done with `strptime`
#strptime(x, format = "%Y/%m/%d %H:%M", tz = "UTC")
#[1] "2003-11-04 19:29:00 UTC" "2001-04-02 21:32:00 UTC" "2003-10-28 09:51:00 UTC"
Or with lubridate
lubridate::ymd_hm(x)
Replace x with column name df$column_name.

Related

Factor variable into weekdays

I have a variable for the date of medical admission. However, it is not properly formatted. It is a factor and formatted as "DDMMYEAR HRMN", like "01012016 1215", which should mean "01-01-2016 12:15". How can I reformat it and assign weekdays?
You can use lubridate to parse the date, then weekdays from base R to get the day of week as a character.
library(lubridate)
d <- dmy_hm("01012016 1215")
weekdays(d)
Use as.POSIXct/strptime to convert to date time and then use weekdays.
df$date <- as.POSIXct(df$date, format = '%d%m%Y %H%M', tz = 'UTC')
df$weekday <- weekdays(df$date)
For example,
string <- '01012016 1215'
date <- as.POSIXct(string, format = '%d%m%Y %H%M', tz = 'UTC')
date
#[1] "2016-01-01 12:15:00 UTC"
weekdays(date)
#[1] "Friday"

Including seconds when using strptime with examples such as 10-10-2010 00:00:00 [duplicate]

This question already has answers here:
How can I keep midnight (00:00h) using strptime() in R?
(2 answers)
Closed 3 years ago.
I have had a good hunt around and sure this has to have been answered before but I cant seem to find any help!
I have a series of times in a data frame, some of which have the following time stamp in the following format:
Date <- '2018-10-10'
Time <- '00:00:00'
When I use the strptime function it returns only the date, it removes the 00:00:00, see below:
datetime <- strptime(paste(Date,Time),
format = "%Y-%m-%d %H:%M:%S",
tz = 'GMT')
> datetime
[1] "2018-10-10 GMT"
if for example it was Time <- 00:00:01 it would return
> datetime
[1] "2018-10-10 00:00:01 GMT"
Does anyone know a way of ensuring the output for 00:00:00 instances are displayed. Desired output:
"2018-10-10 00:00:00 GMT"
Many thanks!!
Jim
When you type datetime and hit <Enter>, R will use a/the suitable print method to display datetime. Just because datetime returns "2018-10-10 GMT" doesn't mean that datetime has forgotten about the seconds.
To ensure a consistent format of your POSIXlt object, you could use format
format(datetime, "%Y-%m-%d %H:%M:%S", usetz = T)
#[1] "2018-10-10 00:00:00 GMT"
Similar for case 2
Date <- '2018-10-10'
Time <- '00:00:01'
datetime <- strptime(paste(Date,Time), format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')
format(datetime, "%Y-%m-%d %H:%M:%S", usetz = T)
#[1] "2018-10-10 00:00:01 GMT"
Sample data
Date <- '2018-10-10'
Time <- '00:00:00'
datetime <- strptime(paste(Date,Time), format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')

Need to convert yyyy-mm-dd hh:mm:ss UTC to yyyy-mm-dd hh:mm format

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'

Convert unconventional datetime string to POSIXct in R

What format string argument of the as.POSIXct() function would allow me to coerce the following timestamp into POSIXct?
datetime <- "2018/02/08T23:58:33z"
datetime <- as.POSIXct(datetime, format = "%Y/%m/%d %H:%M:%S", tz = "UTC)
Desired result
2018-02-08 23:58:33
Just put a literal "T" in the string to match (trailing characters are ignored by default anyway):
as.POSIXct(datetime, format = "%Y/%m/%dT%H:%M:%S", tz = "UTC")
Other option is to use anytime which parses automatically
anytime::anytime(datetime, tz = "UTC", asUTC = TRUE)
#[1] "2018-02-08 23:58:33 UTC"

convert character string with timezone to date in r

I have a vector of character strings looking like this. I want to convert them to dates. The characters for time-zone is posing trouble.
> a
[1] "07/17/2014 5:01:22 PM EDT" "7/17/2014 2:01:05 PM PDT" "07/17/2014 4:00:48 PM CDT" "07/17/2014 3:05:16 PM MDT"
If I use: strptime(a, "%d/%m/%Y %I:%M:%S %p %Z") I get [1] NA
If i omit the "%Z" for time-zone, and use this:
strptime(a, "%m/%d/%Y %I:%M:%S %p", tz = "EST5EDT") I get
[1] "2014-07-17 17:01:22 EDT"
Since my strings contain various time zones - PDT, CDT, EDT, MDT , I can't default all time zones to EST5EDT. One way to overcome is split the vector into different vectors for each time-zone, remove the letters PDT / EDT etc. and apply the right timezone with strptime - "EST5EDT" , "CST6CDT" etc. Is there any other way to solve this?
If the date is always the first part of the elements of the character vector and it is always followed by the time, splitting the elements by the whitespaces is a possibility. If only the date is needed:
dates <- sapply(a, function(x) strsplit(x, split = " ")[[1]][1])
dates <- as.Date(as.character(dates), format = "%m/%d/%Y")
[1] "2014-07-17" "2014-07-17" "2014-07-17" "2014-07-17"
If also the time is needed:
datetime <- sapply(a, function(x) paste(strsplit(x, split = " ")[[1]][1:3],
collapse = " "))
datetime <- strptime(as.character(datetime), format = "%m/%d/%Y %I:%M:%S %p")
[1] "2014-07-17 17:01:22 CEST" "2014-07-17 14:01:05 CEST"
You can set a different timezone using the tz argument here.

Resources