I have a datasets collected by alternative auto-time setting. This is the example format.
file = data.table(ID = c(1:19), time = as.hms("6:01:00 ", "6:03:00 ",
"6:05:00 ", "6:08:00 ",
"6:10:00 ", "6:12:00 ",
"6:15:00 ", "6:17:00 ",
"6:19:00 ", "6:22:00 ",
"6:24:00 ", "6:26:00 ",
"6:29:00 "),
Turn = c("A", "A", "B", "B", "C", "C", "A", "A", "A", "B",
"B", "C", "C"),
Value = c("16.00", "18.00", "17.00", "19.00", "21.00",
"26.00", "25.60", "27.34", "29.09", "30.83",
"32.57", "34.31", "36.06"))
From this dataset, I separated it into three dataframes for each turn by using the following code:
Data_A <- file[file$Turn %in% c("A"), ]
Data_B <- file[file$Turn %in% c("B"), ]
Data_C <- file[file$Turn %in% c("C"), ]
Data_A
ID Time Turn Value
1 6:01:00 A 16.00
2 6:03:00 A 18.00
7 6:15:00 A 25.60
8 6:17:00 A 27.34
9 6:19:00 A 29.09
Data_C
ID Time Turn Value
5 6:10:00 C 21.00
6 6:12:00 C 26.00
12 6:26:00 C 34.31
13 6:29:00 C 36.06
I want to combine Data_A and Data_C by the nearest time as follow:
ID Time Turn Value Time C Turn C Value C
1 6:01:00 A 16.00 6:10:00 C 21.00
2 6:03:00 A 18.00 6:12:00 C 26.00
7 6:15:00 A 25.60 6:26:00 C 34.31
8 6:17:00 A 27.34 6:29:00 C 36.06
9 6:19:00 A 29.09
As you see, there is an extra A in Data_A. I will exclude it. There are so many such irregular data in the time series dataset. Can anyone help me how to code for this result? Thanks!