1

I have a data table, which I have various dates and a "value" with which I need to plot. The values are delivered in a so-called "Delivery Hour" and a "Delivery Interval". An example is below;

enter image description here

I can join a new colum to the right of my table, popultaed (in the first instance) with NAs using

data.table["TIME"]<-NA

enter image description here

This new column (shown below) needs to be populated in each row by the time of day that "value" was delivered, in excel I convert this via the function

=TIME(DELIVERY_HOUR-1, 30*(DELIVERY_INTERVAL-1),0)

So for example, with DELIVERY_HOUR =7 and DELIVERY_INTERVAL=2 this is 06:30.

I'd like to write a function that populates the TIME column row by row, here is my syntax for the function "TIME":

TIME function(x){ time<-(DELIVERY_HOUR-1):(30*(DELIVERY_INTERVAL-1)) result<-paste(time,format="%H%M") return(result) }

I am unsure how to ensure that corresponding row entries are picked and placed into the row entry for TIME.

Any help on this matter would be greatly appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Autolatry
  • 113
  • 3
  • If you could merge it I would be most grateful. Best, A – Autolatry Feb 20 '15 at 11:06
  • Just out of curiosity, if you allow tags to R, why not questions about R? – Autolatry Feb 20 '15 at 11:06
  • if you are blocked at SO, [system simply won't let question migrate](http://meta.programmers.stackexchange.com/q/7020/31260) – gnat Feb 20 '15 at 11:12
  • this is my very first question, I thought it was relevant to this site; there is no reason why I would be blocked at SO. – Autolatry Feb 20 '15 at 11:13
  • if this is the case, you can safely flag for migration (flag -> off-topic -> belongs to other site -> ...). Alternatively, you can delete here and reask at the other site – gnat Feb 20 '15 at 11:16
  • @gnat Thanks, flagged for attention. – Autolatry Feb 20 '15 at 11:18
  • What's with immediately assuming this user is blocked from asking questions on SO? Chill out, it's Friday. We do, however, need actual data and not pictures of data. Please have a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – Rich Scriven Feb 20 '15 at 17:02
  • @RichardScriven ["Per stats, Programmers appear to be most popular of SE sites among this category of askers..."](http://meta.programmers.stackexchange.com/a/6991/31260) – gnat Feb 27 '15 at 08:46

1 Answers1

1

Well if you are actually using the data.table in R you could do the following:

library(data.table)

# minimally reproducible data
dt <- data.table(date=20150202, delivery_date="02/02/2015",
                 delivery_hour = c(rep(7, 4),rep(8,4),9),
                 delivery_interval = c(1,1,2,2,1,1,2,2,1))

dt[, time := paste0(delivery_hour - 1, ":", 30*delivery_interval]
dt
       date delivery_date delivery_hour delivery_interval time
1: 20150202    02/02/2015             7                 1 6:30
2: 20150202    02/02/2015             7                 1 6:30
3: 20150202    02/02/2015             7                 2 6:60
4: 20150202    02/02/2015             7                 2 6:60
5: 20150202    02/02/2015             8                 1 7:30
6: 20150202    02/02/2015             8                 1 7:30
7: 20150202    02/02/2015             8                 2 7:60
8: 20150202    02/02/2015             8                 2 7:60
9: 20150202    02/02/2015             9                 1 8:30

This naturally assumes the 24 hour clock time (delivery hour will not be less than 1 and not exceed 24)

cdeterman
  • 19,630
  • 7
  • 76
  • 100