0

I have 2 data frames:

vector1 <- c(1:24) #listing of hours
vector2 <- seq.int(.0,.60,.05) #listing of every 5 minutes
possible_hrs <- data.frame(outer(vector1,vector2,'+')) 
    #matrix of all possible hours/minutes
possible_hrs


#pull in start/end time possibilities
start_time <-c(5,2)
end_time <-c(8,10)
class_time<-data.frame(start_time,end_time)
class_time

I then want to be able to compare every value from both data frames against one another. I've tried something like this to no avail:

which(possible_hrs >= class_time$start & possible_hrs <= class_time$end, arr.ind=TRUE) 

The end goal is to have a listing of "keys" from the class_time dataframe that corresponds to the appropriate hours of the possible_hrs dataframe, something like:

start   end    time
8:00    8:45   8:00
8:00    8:45   8:05
8:00    8:45   8:10
...
8:00    8:45   8:45
10 Rep
  • 2,217
  • 7
  • 19
  • 33
jschlereth
  • 1,071
  • 2
  • 10
  • 12
  • 2
    Is there are reason you are taking this approach? Or is your end goal all that matters? By "this approach", I mean (a) using decimals instead of standard classes for time, (b) doing relying on this matching approach instead of, say, building sequences from start time to end time, (c) including `2.6` and `3.0` as if they're different times... – Gregor Thomas Aug 31 '18 at 18:40
  • 1
    Are you using the fractional component to indicate "minutes" (so your `8.6` is wrong) or "fractional hours" (so you're missing 8.65 to 8.95)? This often does not end well. – r2evans Aug 31 '18 at 18:40
  • @Gregor, thanks for asking. The end goal is all that matters. I was trying to avoid any unnecessary complications that may be present using specific time formats. And you're right about the 2.6 and 3...I can modify the above script to stop at .55 rather than .6 currently. – jschlereth Aug 31 '18 at 18:43
  • @r2evans, Thank for asking, but no the decimal should represent every 5 minutes...technically not a fraction of the hours. Initially just a representation of the hours/minutes is what I was going for. – jschlereth Aug 31 '18 at 18:44
  • Using non-specific time formats is also problematic. If you are really avoiding the day/date component, then (you may already know that) R has no "time" data type, only `POSIXt` which includes the date. So if you are going to try to represent a date-less time, then I strongly suggest fractional hours. Otherwise, addition/subtraction will not rollover correctly among other things. – r2evans Aug 31 '18 at 18:46
  • 2
    But if that's truly what you want, then: `subset(do.call(rbind.data.frame, lapply(mapply(seq, class_time$start_time, class_time$end_time, by=0.05), function(a) cbind.data.frame(start=min(a), end=max(a), time=a))), time%%1 < 0.59)` gives you something close. – r2evans Aug 31 '18 at 18:46
  • If you go with fractional hours (my suggestion), then `do.call(rbind.data.frame, lapply(mapply(seq, class_time$start_time, class_time$end_time, by=5/60), function(a) cbind.data.frame(start=min(a), end=max(a), time=a)))` will give you the equivalent. – r2evans Aug 31 '18 at 18:50
  • The `times` class in the `chron` package is pretty good for times without dates. [R: How to handle times without dates?](https://stackoverflow.com/q/22659947/903061) shows one example. – Gregor Thomas Aug 31 '18 at 18:52
  • @r2evans, Awesome. Thanks. Your initial script is what I needed to get this working. The overall game plan is just for a visual component that I'm working with which is why the initial decimal approach for minutes was selected. But I appreciate it. – jschlereth Aug 31 '18 at 18:54
  • Please recognize that what you use to calculate and store for these times can be distinct from how you visualize/represent them. Consider: `times <- seq(8,9,by=5/60) ; sprintf("%d:%02d", as.integer(floor(times)), round(60*(times%%1)))`. – r2evans Aug 31 '18 at 19:54

0 Answers0