Add %H:%M:%S into as.POSIXct format in R - r

This looks like trivial issue but I couldn't make it work. I need simply convert midnight into POSIXct format but also with hours, minutes and seconds, just like that:
nextDay_t <- strptime(paste0(as.character(Sys.Date() + 1)," 00:00:00"), format = "%Y-%m-%d %H:%M:%S")
nextDay_t <- format(nextDay_t, "%Y-%m-%d %H:%M:%S")
nextDay_t <- as.POSIXct(nextDay_t, format='%Y-%m-%d %H:%M:%S', tz="EST")
But still have only "2018-12-11 EST" instead of "2018-12-11 00:00:00 EST". Is there anything I'm missing in my code?

Your code seems on the right track. The hour/minute/second components are in fact still there after calling strptime. They just do not show up automatically by default when inspecting the object.
You may try the following call to format, which includes these components, as well as the time zone (%Z):
nextDay_t <- strptime(paste0(as.character(Sys.Date() + 1)," 00:00:00"), format = "%Y-%m-%d %H:%M:%S")
nextDay_t
format(nextDay_t,"%Y/%m/%d %H:%M:%S %Z") # include hour/minute/second and time zone
[1] "2018-12-11 CET"
[1] "2018/12/11 00:00:00 CET"

Related

POSIXct from character returns NA

I have a time in excel that when converter to R, comes as a character and looks someting like this 0.59658.
I am trying to convert to POSIXct but it returns as a POSIXct with NA.
teste <- as.POSIXct(test, format = "%H:%M")
I've also tried teste <- as.POSIXct(test, format = "%H:%M:%S")
For other columns it works fine, but not this one..
UPDATE:
I've done the solution, but a second problem comes with the rest of the thing that I need.
teste <- as.POSIXct(teste*24*60*60,"%H%M", origin="1970-01-01")
teste <- format(as.POSIXct(teste, format = "%Y-%m-%d %H:%M:%S"), format="%H:%M")
And now, I want to paste with a date vector that is a POSIXct in the 2013-01-06, with this command:
teste<-as.POSIXct(paste(date, teste), format="%Y-%m-%d %H:%M:%S")
And the NA are back
Confused as to what exactly you want, but what is wrong with this function:
df <- data.frame(number = c(0.59658, 0.59658, 0.59658, 0.59658, 0.59658), dates = c("2013-01-06", "2013-01-06", "2013-01-06", "2013-01-07", "2013-01-07"))
testing <- function(number, dates){
teste <- as.POSIXct(number*24*60*60,"%H%M", origin="1970-01-01")
teste <- format(as.POSIXct(teste, format = "%Y-%m-%d %H:%M:%OS"), format="%H:%M")
return(as.POSIXct(paste0(dates," ",teste)))
}
Which gives the following when doing testing(df$number, df$dates):
"2013-01-06 14:19:00 EST" "2013-01-06 14:19:00 EST" "2013-01-06 14:19:00 EST" "2013-01-07 14:19:00 EST" "2013-01-07 14:19:00 EST"

Converting seconds from specific time on a specific date in R

If this question has been asked before, please downvote and direct me. I have been looking through SO, but it seems no one has had the need for a non-midnight start time i.e. everyone wants to know how to convert seconds from a specific midnight value.
I'm trying to convert my second values to a data value. What I have are seconds from the time 2017-05-21 22:00.
I tried using the as.POSIXct() function, however it only seem to take Y-m-d into account and disregards if I write h:m after it.
e.g file$date = as.POSIXct(file$Time,origin = "2017-05-21 22:00") gives me
Time date
1 0.00 2017-05-22 00:00:00
I have found if I use
file$Time = file$Time-3600*4
file$date = as.POSIXct(file$Time,origin = "2017-05-22")
for some reason gives me the correct output which is of course
Time date
1 0.00 2017-05-21 22:00:00
Any idea on how to do this more elegantly?
Also, if you have a clue on why that gives me the correct output, I'm all ears.
You can simply try as.POSIXct to convert your starting time and then keep on adding seconds. as:
as.POSIXct("2017-05-21 22:00:00", format = "%Y-%m-%d %H:%M:%S")
#[1] "2017-05-21 22:00:00 BST"
as.POSIXct("2017-05-21 22:00:00", format = "%Y-%m-%d %H:%M:%S") + 1
#[1] "2017-05-21 22:00:01 BST"
as.POSIXct("2017-05-21 22:00:00", format = "%Y-%m-%d %H:%M:%S") + 100
#[1] "2017-05-21 22:01:40 BST"
as.POSIXct("2017-05-21 22:00:00", format = "%Y-%m-%d %H:%M:%S") + 300
#[1] "2017-05-21 22:05:00 BST
You can even specify time-zone using tz parameter as:
as.POSIXct("2017-05-21 22:00:00", tz = "UTC", format = "%Y-%m-%d %H:%M:%S")
#[1] "2017-05-21 22:00:00 UTC"
as.POSIXct("2017-05-21 22:00:00", tz = "UTC", format = "%Y-%m-%d %H:%M:%S") + 96
#[1] "2017-05-21 22:01:36 UTC"
Have a look at lubridate...
library(lubridate)
ymd_hm("2017-05-21 22:00") + seconds(1.01)
So in your case it would be something like
file$date <- ymd_hm("2017-05-21 22:00") + seconds(file$Time)

why local time dosen't work? POSIXct in specific time zone [R]

the time my data are in EST time zone, and I try to use this time zone.
I want to count the week (in local time, not GMT), so I manually define an originTime in EDT
originTime = as.POSIXlt('2000-01-02 00:00:00 EDT')
dt2 = data.frame(time=c(as.POSIXlt('2000-01-09 00:00:05 EDT')))
dt2$week = as.integer( floor( ( as.numeric(dt2$time) - as.numeric(originTime) ) /(3600*24*7) ) )
dt2$wday = weekdays(dt2$time)
This works.
Now I want to find out, what's one week after a given time?
> as.POSIXlt( 1 * 3600*24*7 , origin = originTime)
[1] "2000-01-08 19:00:00 EST"
Here's the problem, R seems to think originTime is in GMT. Can somebody help? Thanks
Two serious problems. EDT does not really exist, and even if it did it would not be appropriate for a January date. The (US) Eastern timezone is "EST5EDT" to make it distinct from the Ozzie EST. (Furthermore these may be different on different OSes.) Safest would be tz="America/New_York". For data entry, you need to use the 'tz' parameter AND change the default (FALSE) setting of 'usetz':
(originTime = as.POSIXlt('2000-01-02 00:00:00', format="%Y-%m-%d %H:%M:%S",
tz="EST5EDT", usetz=TRUE) )
[1] "2000-01-02 EST"
A test using "%Z" which is only for output:
> format( as.POSIXlt('2000-01-02 00:00:00', format="%Y-%m-%d %H:%M:%S",
tz="America/New_York", usetz=TRUE) ,
format="%Y-%m-%d %H:%M:%S %Z")
[1] "2000-01-02 00:00:00 EST"
I've never used the origin argument in as.POSIXlt so cannot really explain why it fails to deliver the expected result I've always used +.POSIXt or seq.POSIXt to construct intervals:
format(as.POSIXlt('2000-01-02 00:00:00', tz="America/New_York", usetz=TRUE)+ 1*3600*24*7,
format="%Y-%m-%d %H:%M:%S %Z") # %Z is only used for output
# [1] "2000-01-09 00:00:00 EST" A week later at midnight.
I think the reason one needs to force a time printing with a format argument is that midnight-times are shortened to just printing the date with no further H:M:S information.

convert local dateTime to UTC in R

How can I convert local DateTime in the following format "12/31/2014 6:42:52 PM" to UTC in R? I tried this
as.POSIXct(as.Date("12/31/2014 6:42:52 PM", format="%m/%d/%Y %H:%M:%S"),tz="UTC")
but it doesn't seem to be valid.
If you want to shift a datetime from your current timezone to UTC, you need to
import in your local timezone, then just shift the display timezone to "UTC". e.g.: in Australian EST I am UTC+10.
out <- as.POSIXct("12/31/2014 6:42:52 PM", format="%m/%d/%Y %H:%M:%S")
out
#"2014-12-31 06:42:52 EST"
#(Australian Eastern Standard Time)
as.numeric(out)
#[1] 1419972172
Now shift the timezone for display purposes:
attr(out, "tzone") <- "UTC"
out
#[1] "2014-12-30 20:42:52 UTC"
# display goes 10 hours backwards as I'm UTC+10
as.numeric(out)
#[1] 1419972172
Note that this doesn't affect the underlying numeric data (seconds since 1970-01-01), it only changes what is displayed.

How do you convert time stamp that has AM/PM at the end of the string?

I am trying to convert this time stamp to POSIXct
t1 <- c("19-Jun-13 06.00.00.00 PM")
If I do this:
t1 <- as.POSIXct(t1, format="%d-%b-%y %H:%M:%S")
whould this convert this time stamp right? Does that considder the AM/PM at the end?
Read ?strptime. %p, which only works with %I, not %H. Your time format is also incorrect. Your times are separated by ".", not ":".
as.POSIXct("19-Jun-13 06.00.00.00 PM", format="%d-%b-%y %I.%M.%OS %p")
I don't understad why strptime doesn't recognize properly the %p format. However, the function dmy_hmsfrom package lubridate works well.
lubridate::dmy_hms("19-Jun-13 06.00.00.00 PM") creates the following result:
[1] "2013-06-19 18:00:00 UTC"
which you can "reformat" if you want, say Y-m-d H:M:
as.POSIXct(dmy_hms("19-Jun-13 06.00.00.00 PM"), format="%Y-%m-%d %H:%M")
[1] "2013-06-19 18:00:00 UTC"

Resources