I have two tables (below) that i need to merge to create the desired results (also below). I know how to do it in SQL, but in this instance I can't. How would I merge the two tables?
LabStations Table
StationNumber
1: 1
2: 2
3: 3
4: 4
5: 5
Calendar Table:
dates
1: 2010-01-01 06:00:00
2: 2010-01-01 07:00:00
3: 2010-01-01 08:00:00
4: 2010-01-01 09:00:00
5: 2010-01-01 10:00:00
6: 2010-01-01 11:00:00
DESIRED RESULTS:
Note: in sql I would get the same results as below query, but i can't use sql for the calendar in this case.:
select s.StationNumber, c.dates
from LabStations, Calendar
Desired result table:
StationNumber dates
1: 1 2010-01-01 06:00:00
2: 1 2010-01-01 07:00:00
3: 1 2010-01-01 08:00:00
4: 1 2010-01-01 09:00:00
5: 1 2010-01-01 10:00:00
6: 2 2010-01-01 06:00:00
7: 2 2010-01-01 07:00:00
8: 2 2010-01-01 08:00:00
9: 2 2010-01-01 09:00:00
10: 2 2010-01-01 10:00:00
...
21: 5 2010-01-01 06:00:00
22: 5 2010-01-01 07:00:00
23: 5 2010-01-01 08:00:00
24: 5 2010-01-01 09:00:00
25: 5 2010-01-01 10:00:00
Code to reproduce the tables:
#Lab stations come from a database so they really aren't sequential.
LabStations <- list(1:5)
setDT(LabStations)
names(LabStations) <- c("StationNumber")
#Dates are really 5 years, not five hours.
dates <- seq.POSIXt(ISOdatetime(2010,1,1,0,0,0,tz="America/Chicago"), ISOdatetime(2010,1,1,4,0,0,tz="America/Chicago"), by="hour")
Calendar <- data.frame(dates)
setDT(Calendar)
attributes(Calendar$dates)$tzone <- "UTC"
#MergedTable <- ????