Generate a working day sequence in R - r

I want to generate a working week / working day sequence (Monday-Friday; 8am - 5pm) in R. However I only figured out how to extract a working week (Monday-Friday) with 24 hours.
library(timeDate)
start <- as.POSIXct("2010-01-01")
interval <- 60
seq_1 <- as.timeDate(seq(from=start, by=interval*60, length.out = 200))
seq_2 <- seq_1[isWeekday(seq_1)]; seq_2
dayOfWeek(seq_2)
Is there a similar function which can extract only working hours? Thanks

You can use function format to obtain hours
seq_2[as.numeric(format(seq_2,'%H')) %in% 8:15 ]

Select weekdays and then repeat with frequency equal to the desired hours. I'm afraid I missed your 8 o;clock start and used the phrase "9 to 5" as my guide:
twoyears <- seq.Date(as.Date("2010-01-01"), by='day', length.out=365*2)
twoworkyrs <- twoyears[isWeekday(twoyears, wday = 1:5)]
twoworkyrs[ 1:10]
# [1] "2010-01-01" "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" "2010-01-08"
# [7] "2010-01-11" "2010-01-12" "2010-01-13" "2010-01-14"
workhours <- as.POSIXct( as.numeric(rep(twoworkyrs, each=9))*24*3600 + # weekdays
(9:17)*3600 , n # working hours
origin="1970-01-01", tz="America/LosAngeles")
#----- First two weeks ----------------
> workhours[1:90]
[1] "2010-01-01 09:00:00 UTC" "2010-01-01 10:00:00 UTC" "2010-01-01 11:00:00 UTC"
[4] "2010-01-01 12:00:00 UTC" "2010-01-01 13:00:00 UTC" "2010-01-01 14:00:00 UTC"
[7] "2010-01-01 15:00:00 UTC" "2010-01-01 16:00:00 UTC" "2010-01-01 17:00:00 UTC"
[10] "2010-01-04 09:00:00 UTC" "2010-01-04 10:00:00 UTC" "2010-01-04 11:00:00 UTC"
[13] "2010-01-04 12:00:00 UTC" "2010-01-04 13:00:00 UTC" "2010-01-04 14:00:00 UTC"
[16] "2010-01-04 15:00:00 UTC" "2010-01-04 16:00:00 UTC" "2010-01-04 17:00:00 UTC"
[19] "2010-01-05 09:00:00 UTC" "2010-01-05 10:00:00 UTC" "2010-01-05 11:00:00 UTC"
[22] "2010-01-05 12:00:00 UTC" "2010-01-05 13:00:00 UTC" "2010-01-05 14:00:00 UTC"
[25] "2010-01-05 15:00:00 UTC" "2010-01-05 16:00:00 UTC" "2010-01-05 17:00:00 UTC"
[snipped
I must admit that timezone conversions are one of my weakest suits.

Related

remove time from POSIXct Date

I have convert my date from chr to POSIXCT using formula below.
crime2$Date = parse_date_time(crime2$Date, orders = c('dmy_HM'),tz="UTC")
so my date actually now in this format.
> head(crime2$Date, 10)
[1] "2015-03-18 19:44:00 UTC" "2015-03-18 22:45:00 UTC"
[3] "2015-03-18 22:30:00 UTC" "2015-03-18 22:00:00 UTC"
[5] "2015-03-18 23:00:00 UTC" "2015-03-18 21:35:00 UTC"
[7] "2015-03-18 22:50:00 UTC" "2015-03-18 23:40:00 UTC"
[9] "2015-03-18 23:30:00 UTC" "2015-03-18 22:45:00 UTC"
However, if i want to remove the time and keep the date only, what can i do about this?
Example, they will look like this
" 2015-03-18 " "2015-03-18 "

lubridate:floor_date set reference start timestamp

I'm trying to floor continuous timestamps to 'every x hours' with lubridate:floor_date. However, when my time interval is greater than the hour of the first timestamp, it floors relative to midnight instead of my first timestamp. I have not found a way to set a reference timestamp for my start time. I have timestamps in UTC but need to floor them relative to for example 6:00 and 18:00 local time, which would be 12 hour intervals when referenced to local midnight, but doesn't work for UTC time when it keeps referencing to (UTC) midnight.
I know I could convert my timestamps to local time, but that is less than ideal. Is there a way to define the reference timestamps for floor_date that I'm missing?
Basically, what I'd like to do is floor the timestamps "every hour" relative to the start of my timeseries instead of each timestamp individually flooring relative to its midnight.
timestamps<-structure(c(1578628800, 1578632400, 1578636000, 1578639600, 1578643200,
1578646800, 1578650400, 1578654000, 1578657600, 1578661200), class = c("POSIXct",
"POSIXt"), tzone = "UTC")
floor_date(timestamps, '4 hours')
[1] "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC"
[4] "2020-01-10 04:00:00 UTC" "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC"
[7] "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC" "2020-01-10 12:00:00 UTC"
[10] "2020-01-10 12:00:00 UTC"
floor_date(timestamps, '5 hours')
[1] "2020-01-10 00:00:00 UTC" "2020-01-10 05:00:00 UTC" "2020-01-10 05:00:00 UTC"
[4] "2020-01-10 05:00:00 UTC" "2020-01-10 05:00:00 UTC" "2020-01-10 05:00:00 UTC"
[7] "2020-01-10 10:00:00 UTC" "2020-01-10 10:00:00 UTC" "2020-01-10 10:00:00 UTC"
[10] "2020-01-10 10:00:00 UTC"
Try the clock package:
clock::date_floor(timestamps, 'hour', n = 4)
[1] "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC"
[3] "2020-01-10 04:00:00 UTC" "2020-01-10 04:00:00 UTC"
[5] "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC"
[7] "2020-01-10 08:00:00 UTC" "2020-01-10 08:00:00 UTC"
[9] "2020-01-10 12:00:00 UTC" "2020-01-10 12:00:00 UTC"
clock::date_floor(timestamps, 'hour', n = 5)
[1] "2020-01-10 01:00:00 UTC" "2020-01-10 01:00:00 UTC"
[3] "2020-01-10 06:00:00 UTC" "2020-01-10 06:00:00 UTC"
[5] "2020-01-10 06:00:00 UTC" "2020-01-10 06:00:00 UTC"
[7] "2020-01-10 06:00:00 UTC" "2020-01-10 11:00:00 UTC"
[9] "2020-01-10 11:00:00 UTC" "2020-01-10 11:00:00 UTC"

Generate an ordered series of datetime

I am working in R.
I have to generate a series of dates and times. In particular, I would like to have two data points per day, hence to assign twice each date with a different time, for instance:
"2001-05-13 00:00:00"
"2001-05-13 12:00:00"
"2001-05-14 00:00:00"
"2001-05-14 12:00:00"
I found the following code to produce a series of dates:
seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by = 0.5)
Nevertheless, even if I set the by = 0.5, the code returns only a date , not a datetime.
Any idea how to produce a series of datetimes?
as.Date will produce only dates, use as.POSIXct to produce date-time.
seq(as.POSIXct("2000-01-01 00:00:00", tz = 'UTC'),
as.POSIXct("2003-01-01 00:00:00", tz = 'UTC'), by = '12 hours')
# [1] "2000-01-01 00:00:00 UTC" "2000-01-01 12:00:00 UTC"
# [3] "2000-01-02 00:00:00 UTC" "2000-01-02 12:00:00 UTC"
# [5] "2000-01-03 00:00:00 UTC" "2000-01-03 12:00:00 UTC"
# [7] "2000-01-04 00:00:00 UTC" "2000-01-04 12:00:00 UTC"
# [9] "2000-01-05 00:00:00 UTC" "2000-01-05 12:00:00 UTC"
#[11] "2000-01-06 00:00:00 UTC" "2000-01-06 12:00:00 UTC"
#[13] "2000-01-07 00:00:00 UTC" "2000-01-07 12:00:00 UTC"
#...
#...

Sequence of only time (no dates) in r

I am trying to make a sequence that only consists of times with one hour interval, without dates. It should look like this:
"00:00:00" "1:00:00" "2:00:00" "3:00:00"
I know that this code works:
dat <- seq(
from=as.POSIXct("00:00:00","%H:%M:%S", tz="UTC"),
to=as.POSIXct("23:00:00", "%H:%M:%S", tz="UTC"),
by="hour"
)
Which gives
[1] "2018-04-10 00:00:00 UTC" "2018-04-10 01:00:00 UTC" "2018-04-10 02:00:00 UTC" "2018-04-10 03:00:00 UTC" "2018-04-10 04:00:00 UTC"
[6] "2018-04-10 05:00:00 UTC" "2018-04-10 06:00:00 UTC" "2018-04-10 07:00:00 UTC" "2018-04-10 08:00:00 UTC" "2018-04-10 09:00:00 UTC"
[11] "2018-04-10 10:00:00 UTC" "2018-04-10 11:00:00 UTC" "2018-04-10 12:00:00 UTC" "2018-04-10 13:00:00 UTC" "2018-04-10 14:00:00 UTC"
[16] "2018-04-10 15:00:00 UTC" "2018-04-10 16:00:00 UTC" "2018-04-10 17:00:00 UTC" "2018-04-10 18:00:00 UTC" "2018-04-10 19:00:00 UTC"
[21] "2018-04-10 20:00:00 UTC" "2018-04-10 21:00:00 UTC" "2018-04-10 22:00:00 UTC" "2018-04-10 23:00:00 UTC"
But that is not what I want. Therefore I tried
library(chron)
seq(from = times("00:00:00"), to =times("23:00:00"), by="hour")
which gives an error
Error in convert.times(times., fmt) : format h:m:s may be incorrect
In addition: Warning message:
In unpaste(times, sep = fmt$sep, fnames = fmt$periods, nfields = 3) :
wrong number of fields in entry(ies) 1
I am stuck now, so I hope somebody can help me with this.
Of course I could just type it out, but I want to have a clean solution.
Using package chron which provides a times class:
library(chron)
times("00:00:00") + (0:23)/24
#[1] 00:00:00 01:00:00 02:00:00 03:00:00 04:00:00 05:00:00 06:00:00 07:00:00 08:00:00 09:00:00 10:00:00 11:00:00 12:00:00 13:00:00 14:00:00
#[16] 15:00:00 16:00:00 17:00:00 18:00:00 19:00:00 20:00:00 21:00:00 22:00:00 23:00:00
You can use strftime() to extract values in any format to character:
dat <- seq(
from=as.POSIXct("00:00:00","%H:%M:%S", tz="UTC"),
to=as.POSIXct("23:00:00", "%H:%M:%S", tz="UTC"),
by="hour"
)
strftime(dat, format="%H:%M:%S")
#"02:00:00" "03:00:00" "04:00:00" "05:00:00" "06:00:00" "07:00:00"
#"08:00:00" "09:00:00" "10:00:00" "11:00:00" "12:00:00" "13:00:00"
#"14:00:00" "15:00:00" "16:00:00" "17:00:00" "18:00:00" "19:00:00"
#"20:00:00" "21:00:00" "22:00:00" "23:00:00" "00:00:00" "01:00:00"
When you have a POSIXct class,
to extract only the hour, minutes and seconds you just need to do:
as.character(format(from, "%H:%M:%S"))
as.character(format(to, "%H:%M:%S"))

R - How to calculate in a new column the difference in seconds between the first and the remaining dates

I have the following dates and I want to calculate the difference between the first date and the other dates. e.g. The difference must be date 2- date 1, date 3 - date 1 etc, in seconds and in another column.
Any help is appreciated I am new in R.
"2009-06-01 16:00:00 UTC"
"2009-06-29 16:00:00 UTC"
"2009-06-29 17:00:00 UTC"
"2009-06-30 16:00:00 UTC"
"2009-06-30 17:00:00 UTC"
"2009-06-30 18:00:00 UTC"
"2009-06-30 19:00:00 UTC"
"2009-07-01 08:00:00 UTC"
"2009-07-01 09:00:00 UTC"
"2009-07-01 10:00:00 UTC"
"2009-07-01 16:00:00 UTC"
"2009-07-01 17:00:00 UTC"
"2009-07-01 18:00:00 UTC"
"2009-07-01 19:00:00 UTC"
"2009-07-02 08:00:00 UTC"
"2009-07-02 09:00:00 UTC"
"2009-07-02 10:00:00 UTC"
"2009-07-02 16:00:00 UTC"
"2009-07-02 17:00:00 UTC"
"2009-07-02 18:00:00 UTC"
"2009-07-02 19:00:00 UTC"
"2009-07-04 10:00:00 UTC"
"2009-07-04 16:00:00 UTC"
"2009-07-04 17:00:00 UTC"
"2010-06-22 16:00:00 UTC"
"2010-06-22 17:00:00 UTC"
"2010-06-22 18:00:00 UTC"
"2010-08-20 16:00:00 UTC"
"2011-06-02 16:00:00 UTC"
"2011-06-02 17:00:00 UTC"
"2011-06-02 18:00:00 UTC"
"2011-06-03 10:00:00 UTC"
"2011-06-03 16:00:00 UTC"
"2011-06-03 17:00:00 UTC"
"2011-06-03 18:00:00 UTC"
"2011-06-03 19:00:00 UTC"
First you'll want to convert your character strings to dates. Once you've done this, you can easily use difftime() to calculate time distances.
There are a number of packages that help you with this and even more ways to do so. So in addition to the answer provided using the lubridate package, here is a way to solve it in base R:
# (I'll assume your data is saved in a vector called my_dates)
my_dates <- gsub(" UTC", "", my_dates) # removes " UTC" from all your dates (for no reason, see edit below)
my_dates <- as.POSIXlt(df$date) # converts to date format
difftime(time1 = my_dates, time2 = my_dates[1], units = "sec")
Time differences in secs
# [1] 0 2419200 2422800 2505600 2509200 2512800 2516400 2563200 2566800 2570400 2592000 2595600
# [13] 2599200 2602800 2649600 2653200 2656800 2678400 2682000 2685600 2689200 2829600 2851200 2854800
# [25] 33350400 33354000 33357600 38448000 63158400 63162000 63165600 63223200 63244800 63248400 63252000 63255600
Note: In my initial answer, I used as.Date.character(), but this ignored the times after the dates! as.Date() also ignores the time and only focuses on the dates. POSIXlt() does the job and keeps both the times and the dates.
Edit from comment: Apparently difftime() is clever enough to recognise strings as dates and automatically gets the right format for the dates, too!:
difftime(my_dates, my_dates[1], units = "secs")
# Time differences in secs
# [1] 0 2419200 2422800 2505600 2509200 2512800 2516400 2563200 # 2566800 2570400 2592000 2595600
# [13] 2599200 2602800 2649600 2653200 2656800 2678400 2682000 2685600 2689200 2829600 2851200 2854800
# [25] 33350400 33354000 33357600 38448000 63158400 63162000 63165600 63223200 63244800 63248400 63252000 63255600
The lubridate package is your friend in this scenario:
library(lubridate)
d <- read.table(text='"2009-06-01 16:00:00 UTC"
"2009-06-29 16:00:00 UTC"
"2009-06-29 17:00:00 UTC"
"2009-06-30 16:00:00 UTC"
"2009-06-30 17:00:00 UTC"
"2009-06-30 18:00:00 UTC"
"2009-06-30 19:00:00 UTC"
"2009-07-01 08:00:00 UTC"
"2009-07-01 09:00:00 UTC"
"2009-07-01 10:00:00 UTC"
"2009-07-01 16:00:00 UTC"
"2009-07-01 17:00:00 UTC"
"2009-07-01 18:00:00 UTC"
"2009-07-01 19:00:00 UTC"
"2009-07-02 08:00:00 UTC"
"2009-07-02 09:00:00 UTC"
"2009-07-02 10:00:00 UTC"
"2009-07-02 16:00:00 UTC"
"2009-07-02 17:00:00 UTC"
"2009-07-02 18:00:00 UTC"
"2009-07-02 19:00:00 UTC"
"2009-07-04 10:00:00 UTC"
"2009-07-04 16:00:00 UTC"
"2009-07-04 17:00:00 UTC"
"2010-06-22 16:00:00 UTC"
"2010-06-22 17:00:00 UTC"
"2010-06-22 18:00:00 UTC"
"2010-08-20 16:00:00 UTC"
"2011-06-02 16:00:00 UTC"
"2011-06-02 17:00:00 UTC"
"2011-06-02 18:00:00 UTC"
"2011-06-03 10:00:00 UTC"
"2011-06-03 16:00:00 UTC"
"2011-06-03 17:00:00 UTC"
"2011-06-03 18:00:00 UTC"
"2011-06-03 19:00:00 UTC"', stringsAsFactors=FALSE)
d <- ymd_hms(d[, 1])
sapply(d, function(x) x-d)

Resources