I've got two dataframes (df1 and df2), an example of which looks like these:
df1 <- data.frame(StationID = c(1,1,1,2,2,3,3,3,3,3),
Cameras = c("Cam1","Cam2","Cam2","Cam1","Cam1","Cam2","Cam1","Cam2","Cam1","Cam1"),
Start = c("2013-04-23","2013-04-23","2013-04-23","2013-04-23","2013-04-23","2013-04-23","2013-04-23","2013-04-23","2013-04-23","2013-04-23"),
End = c("2013-04-25","2013-04-25","2013-04-25","2013-04-25","2013-04-25","2013-04-25","2013-04-25","2013-04-25","2013-04-25","2013-04-25"))
df2 <- data.frame(StationID = c(1,1,2,2,3,3),
Cameras = c("Cam1","Cam2","Cam1","Cam2","Cam1","Cam2"))
I want to produce a new dataframe (df3), which looks for a match between two columns (StationID and Cameras) and then appends "Start" and "End" date columns to the corresponding matches. The code needs to dynamically add new columns based on the data, since some instances will have no matches, whilst others will have many matches.
Example output below:
StationID Cameras Start1 End1 Start2 End2 Start3 End3
1 1 Cam1 2013-04-23 2013-04-25 <NA> <NA> <NA> <NA>
2 1 Cam2 2013-04-23 2013-04-25 2013-04-23 2013-04-25 <NA> <NA>
3 2 Cam1 2013-04-23 2013-04-25 2013-04-23 2013-04-25 <NA> <NA>
4 2 Cam2 <NA> <NA> <NA> <NA> <NA> <NA>
5 3 Cam1 2013-04-23 2013-04-25 2013-04-23 2013-04-25 2013-04-23 2013-04-25
6 3 Cam2 2013-04-23 2013-04-25 2013-04-23 2013-04-25 <NA> <NA>
I'd appreciate any help on this task.
Thanks in advance!