I have one dataset that has a column with 200k rows and every row presents different timestamps.
Example:
02/20/2019 01:30:00 PM
15/02/2019 13:30:00
I have tried to use in R Studio:
dataset <-as.POSIXlt(dataset,format= "%m/%d/%Y %H:%M")
dataset <-as.POSIXlt(dataset,format= "%H:%M:%S")
dataset <-as.POSIXlt(dataset,format= "%m/%d/%Y %I:%M:%S")
But the value changed for "01:30:00" what is considered "AM" or sometimes it brings "NA" as a result.
Do you know if there is another way?
See the parse_date_time() function from the lubridate package:
dat <- c("02/20/2019 01:30:00 PM", "15/02/2019 13:30:00")
lubridate::parse_date_time(dat, orders = c("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %I:%M:%S %p"))
[1] "2019-02-20 13:30:00 UTC" "2019-02-15 13:30:00 UTC"
Note that if you have other combinations, such month/day/year with the 24 hour clock time, you'll have to add that specification in the orders argument:
dat2 <- c("20/02/2019 01:30:00 PM", "15/02/2019 13:30:00")
lubridate::parse_date_time(dat2, orders = c("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %I:%M:%S %p")) # Wrong
[1] "2019-02-20 01:30:00 UTC" "2019-02-15 13:30:00 UTC"
lubridate::parse_date_time(dat2, orders = c("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %I:%M:%S %p", "%d/%m/%Y %I:%M:%S %p")) # Right
[1] "2019-02-20 13:30:00 UTC" "2019-02-15 13:30:00 UTC"
Related
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"
I have data value
dput(a)
"1/3/2019 15:59"
I need to round the time to to the next hour. I need this date to be "1/3/2019 16:00"?
How can I do this?
We can use lubridate dmy_hm to convert to datetime object and then use ceiling_date to convert it to next hour.
library(lubridate)
ceiling_date(dmy_hm("1/3/2019 15:59"), "hour")
#[1] "2019-03-01 16:00:00 UTC"
Use round.POSIXt. No packages are used.
x <- as.POSIXct("1/3/2019 15:59", format = "%m/%d/%Y %H:%M")
round(x + 3600/2 - !(as.numeric(x) %% 3600), "hours")
## [1] "2019-01-03 16:00:00 EST"
I am trying to parse Date-time in AM/PM format in R. I found that '%p' can handle this. However, when I try this:
mydate <- as.POSIXct("01.01.1970 01:00:00 PM", format="%d.%m.%Y %H:%M:%S %p", tz = "UTZ")
mydate
[1] "1970-01-01 01:00:00 UTZ"
> as.numeric(mydate)
[1] 3600
This is clearly 1 AM. I would have expected the output:
[1] "1970-01-01 13:00:00 UTZ"
[1] 46800
What am I missing?
It is considering as 1 AM instead of 1 PM, hence you get 3600 as output.
as.POSIXct("01.01.1970 01:00:00 PM", format="%d.%m.%Y %H:%M:%S %p", tz = "UTC")
#[1] "1970-01-01 01:00:00 UTC"
The document at ?strptime says
%p
AM/PM indicator in the locale. Used in conjunction with %I and not with %H. An empty string in some locales (and the behaviour is undefined if used for input in such a locale).
You need to use %I instead of %H
mydate <- as.POSIXct("01.01.1970 01:00:00 PM", format="%d.%m.%Y %I:%M:%S %p",
tz = "UTC")
as.numeric(mydate)
#[1] 46800
An alternative with lubridate
library(lubridate)
seconds(mdy_hms("01.01.1970 01:00:00 PM"))
#[1] "46800S"
I want to get a time sequence without using dates in R. I googled it but didn't find anything . I have one approach of doing this but that is too with dates.
a <- seq(from=as.POSIXct("2012-01-01 00:00:00", tz="UTC"),
to=as.POSIXct("2012-01-01 05:00:00", tz="UTC"), by="hour")
a <- strftime(a, format="%H:%M:%S", tz = "UTC")
a <- times(a)
a
[1] 00:00:00 01:00:00 02:00:00 03:00:00 04:00:00 05:00:00
I just want to know is there exist any approach that produce a time sequence (w/o using dates)
The "times" class of chron can represent times below 24:00:00 without dates:
library(chron)
times(0:5/24)
## [1] 00:00:00 01:00:00 02:00:00 03:00:00 04:00:00 05:00:00
Maybe
substr(as.character(a),12,19)
OR
sapply(strsplit(as.character(a)," "), "[[", 2)
I have a timestamp vector like
time_stamp <- c("7/1/2013", "7/1/2013 12:00:30 AM", "7/1/2013 12:01:00 AM", "7/1/2013 12:01:30 AM", "8/1/2013","8/1/2013 11:02:30 PM")
I want to format this to date class. I tried
strptime(time_stamp, format = "%d/%m/%Y %H:%M:%S", tz = "GMT")
but since two timestamps have missing times it results in NAs, which should be substituted by default: 12:00:00.
I can run a loop such as:
for (i in 1:length(time_stamp))
{
if(nchar(time_stamp[i])<11)
{
time_stamp[i] <- paste(time_stamp[i], " 12:00:00 AM")
}
}
time_stamp <- format(strptime(time_stamp, format = "%d/%m/%Y %I:%M:%S %p", tz = "GMT"), "%d/%m/%Y %H:%M:%S", tz = "GMT")
Is there a faster and cleaner way to accomplish this? The vector is a part of large dataset so I don't want to loop over it.
lubridate::parse_date_time can take multiple token orders, with or without the %:
lubridate::parse_date_time(time_stamp, orders = c("dmy IMS p", "dmy"))
## [1] "2013-01-07 00:00:00 UTC" "2013-01-07 00:00:30 UTC" "2013-01-07 00:01:00 UTC"
## [4] "2013-01-07 00:01:30 UTC" "2013-01-08 00:00:00 UTC" "2013-01-08 23:02:30 UTC"
Or use its truncated parameter:
lubridate::parse_date_time(time_stamp, orders = 'dmy IMS p', truncated = 4)
which returns the same thing.
Or use a bit of regex replacement and then process as normal:
as.POSIXct(sub("(\\d{4}$)", "\\1 00:00:00", time_stamp),
format = "%d/%m/%Y %H:%M:%S", tz = "GMT")
#[1] "2013-01-07 00:00:00 GMT" "2013-01-07 12:00:30 GMT" "2013-01-07 12:01:00 GMT"
#[4] "2013-01-07 12:01:30 GMT" "2013-01-08 00:00:00 GMT" "2013-01-08 11:02:30 GMT"