1

I'm trying to write a script that will scan a table for times, and those that happen to be past 6pm will be changed to be 6am of the following day. I have tried using the lubridate package (ymd_hms), but the problem is that it forces me to specify a date (I would like to just use the current system date).

I am kind of new to R (and programming in general) so I'm having trouble wrapping my head around how factors, variables and all that works.

endTime <- ymd_hms("x 18:00:00", tz = "America/Chicago")

Ideally I would want the "x" to take on the system date (no time), but lubridate won't let me do that as it only wants a numerical date in there, it won't let me assign some date to a name and use that.

After that, this should happen

for (Time in firstTen) {
  if (tables$Time > endTime ) {
    dateTime = ymd_hms("x+1 06:00:00")
  } 
}

I know the code isn't functional but I just want to give you an idea of what I have in mind.

Any help appreciated!

deezydaisy
  • 21
  • 5
  • You can get the system date with `Sys.Date()` (or in `lubridate` with `today()`). So you can do something like `ymd_hms(paste(Sys.Date(), "6:00:00"), tz = "America/Chicago")`. But to make your script a little more reproducible, I'd recommend picking a fixed day so your results will be the same whenever you run it. Maybe `ymd_hms("2018-01-01 16:00:00", tz = "America/Chicago")` – Gregor Thomas Mar 28 '18 at 14:17
  • Note that `Sys.Date() + 1` works fine, or in the hard-coded method you would hard-code the next day. I like the hardcoding because picking a regular day avoids any problems of leap years, daylight savings time switches, etc., that could cause irregularities on certain days. – Gregor Thomas Mar 28 '18 at 14:20
  • Also note that you say 6pm is your cut-off time, but your code uses 16:00, which is 4pm. – Gregor Thomas Mar 28 '18 at 14:23
  • @Gregor That doesn't work for me. Using your exact code, and trying it with `today()` didn't work either. Both gave me a `Warning message: All formats failed to parse. No formats found.` Also corrected the 16:00 to 18:00, thanks. Edit: Found why it didn't work. " in the wrong place. – deezydaisy Mar 28 '18 at 14:23
  • Weird, both work just fine for me. I am in a fresh R session with `lubridate` loaded. What's your lubridate version? Are you sure you got all the parentheses right? – Gregor Thomas Mar 28 '18 at 14:24
  • Yeah I just realised I had a " around the `today()` from the `6:00:00"` part. – deezydaisy Mar 28 '18 at 14:30

2 Answers2

0

Here you go mate

dateTime = ymd_hms( paste(Sys.Date()+1, "06:00:00", sep="-"))

EDIT: for your other question regarding changing timezones, you can use this: (from here)

require(lubridate)
dateTime = ymd_hms( paste(Sys.Date()+1, "06:00:00", sep="-"))
dateTime <- as.POSIXct(dateTime, tz="Europe/London")  
attributes(dateTime)$tzone <- "America/Los_Angeles"  
dateTime
gaut
  • 5,771
  • 1
  • 14
  • 45
0

You can achieve this with

library(lubridate)
library(dplyr)

endTime <- ymd_hms(paste(Sys.Date(), "18:00:00"), tz = "America/Chicago")
test.data <- data.frame("Original.time" = endTime + minutes(round(rnorm(15, 1440, 2000))))

time.hours <- hour(test.data$Original.time) + 
  minute(test.data$Original.time)/60 + 
  second(test.data$Original.time)/3600
test.data$New.Time <- if_else(time.hours > 18,
                   ymd_hms(paste(date(test.data$Original.time)+1, "6:00:00"), tz = "America/Chicago"),
                   test.data$Original.time)

I hope this helps!

smanski
  • 541
  • 2
  • 7