1

Want to create a seq of minutes from starting date to end date, but leave out Saturday and Sunday in the output.

I can create the sequence, but don't know how to leave out saturdays and sundays..

And also do not want the seconds part in the output, as well as the "IST" part.

seq.POSIXt( as.POSIXct( "2019/06/21"), as.POSIXct( "2019/06/30"), "min")

If there is any other function or method to do it any faster, or simpler, would be much appreciated. TIA

thelatemail
  • 91,185
  • 12
  • 128
  • 188
VjSwamy
  • 79
  • 4
  • Several options here for identifying weekends / weekdays - https://stackoverflow.com/a/26441992/496803 - e.g. `s[!format(s,"%u") %in% c(6,7)]` from over there which will get rid of Saturday/Sunday. – thelatemail Jun 25 '19 at 00:39
  • newbie, and am in a hurry, will do my searching next time around. thanks.. – VjSwamy Jun 25 '19 at 01:32

2 Answers2

1

Using the library timeDate helps things:

library(timeDate)

have = seq.POSIXt( as.POSIXct( "2019/06/21"), as.POSIXct( "2019/06/30"), "min")
want = have[isWeekday(have)]
spazznolo
  • 747
  • 3
  • 9
0

After generating the sequence, you can use weekdays to filter out days other than Saturday and Sunday and then format to get output in desired format.

all_time <- seq.POSIXt( as.POSIXct( "2019/06/21"), as.POSIXct( "2019/06/30"), "min")
format(all_time[!weekdays(all_time) %in% c("Saturday", "Sunday")], "%Y-%m-%d %H:%M")
# [1] "2019-06-21 00:00" "2019-06-21 00:01" "2019-06-21 00:02" "2019-06-21 00:03" 
#     "2019-06-21 00:04" "2019-06-21 00:05" "2019-06-21 00:06" "2019-06-21 00:07" 
#     "2019-06-21 00:08" "2019-06-21 00:09" ......

As @thelatemail points out that weekdays is locale-dependent and the above would not work if you have non-English locale. We can instead use weekday to remove weekends.

format(all_time[!format(all_time, "%u") %in% c(6, 7)], "%Y-%m-%d %H:%M")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213