Depending on the structure of the input data and the expected ressult, the OP has several choices.
From the question and the sample dataset, it is not fully clear what the expected result should look like if the input data contain gaps, i.e., intervals longer than 15 minutes where no data have been recorded. How does the OP wants the gaps in the input data to be reflected in the result?
EDIT: The OP has provided two slightly different datasets. Both are used below to demonstrate the impact of the input data on the result.
The variants below will use lubridate
and data.table
. It is assumed that df
is already ordered by Timesstamp
.
Preparation
This is needed for all variants:
library(lubridate)
library(data.table)
setDT(df)[, Timestamp := mdy_hms(Timestamp)]
Aggregate to next 15 min interval (with gaps in result)
The simplest solution is to aggegate to the next 15 min interval:
df[, .SD[.N], by = .(Interval = ceiling_date(Timestamp, "15 min"))]
Interval Value..kW.
1: 2018-08-12 23:00:00 51
2: 2018-08-13 00:00:00 52
3: 2018-08-13 00:15:00 55
4: 2018-08-13 00:30:00 57
5: 2018-08-13 00:45:00 60
6: 2018-08-13 01:00:00 61
7: 2018-08-13 01:15:00 62
Note that between row 1 and 2 there is a gap of 1 hour where 3 intervals are missing.
For the sake of completeness, here is a variant which works also with unordered data.
df[, .SD[which.max(Timestamp)], keyby = .(Interval = ceiling_date(Timestamp, "15 min"))]
EDIT: With the other dataset (without truncated seconds) we get
df0[, .SD[.N], by = .(Interval = ceiling_date(Timestamp, "15 min"))]
1: 2018-08-12 23:15:00 51
2: 2018-08-13 00:15:00 55
3: 2018-08-13 00:30:00 57
4: 2018-08-13 00:45:00 60
5: 2018-08-13 01:00:00 61
6: 2018-08-13 01:15:00 62
Please, note that without truncated seconds the values are moved to next interval.
Aggregate to next 15 min interval without gaps in result
step <- "15 min"
df[, .SD[.N], by = .(Interval = ceiling_date(Timestamp, step))][
.(seq(min(Interval), max(Interval), step)), on = .(Interval = V1)]
Here we join a sequence of timestamps to complete the missing intervals:
Interval Value..kW.
1: 2018-08-12 23:00:00 51
2: 2018-08-12 23:15:00 NA
3: 2018-08-12 23:30:00 NA
4: 2018-08-12 23:45:00 NA
5: 2018-08-13 00:00:00 52
6: 2018-08-13 00:15:00 55
7: 2018-08-13 00:30:00 57
8: 2018-08-13 00:45:00 60
9: 2018-08-13 01:00:00 61
10: 2018-08-13 01:15:00 62
Now the gap becomes visible in the result by the NA
values.
EDIT: With the other dataset (without truncated seconds) we get
df0[, .SD[.N], by = .(Interval = ceiling_date(Timestamp, step))][
.(seq(min(Interval), max(Interval), step)), on = .(Interval = V1)]
Interval Value..kW.
1: 2018-08-12 23:15:00 51
2: 2018-08-12 23:30:00 NA
3: 2018-08-12 23:45:00 NA
4: 2018-08-13 00:00:00 NA
5: 2018-08-13 00:15:00 55
6: 2018-08-13 00:30:00 57
7: 2018-08-13 00:45:00 60
8: 2018-08-13 01:00:00 61
9: 2018-08-13 01:15:00 62
Rolling join (gaps filled with data in result)
This is a streamlined version of Matt's approach
step = "15 min"
df[.(seq(floor_date(min(Timestamp), step), ceiling_date(max(Timestamp), step),by = step)),
on = .(Timestamp = V1), roll = TRUE]
Timestamp Value..kW.
1: 2018-08-12 23:00:00 51
2: 2018-08-12 23:15:00 51
3: 2018-08-12 23:30:00 51
4: 2018-08-12 23:45:00 51
5: 2018-08-13 00:00:00 52
6: 2018-08-13 00:15:00 55
7: 2018-08-13 00:30:00 57
8: 2018-08-13 00:45:00 60
9: 2018-08-13 01:00:00 61
10: 2018-08-13 01:15:00 62
Here, the gap is filled with data which were copied from the latest available value. From looking at the result, it is no longer visible that there was a gap in the input data.
EDIT: With the other dataset (without truncated seconds) we get
df0[.(seq(floor_date(min(Timestamp), step), ceiling_date(max(Timestamp), step),by = step)),
on = .(Timestamp = V1), roll = TRUE]
Timestamp Value..kW.
1: 2018-08-12 23:00:00 NA
2: 2018-08-12 23:15:00 51
3: 2018-08-12 23:30:00 51
4: 2018-08-12 23:45:00 51
5: 2018-08-13 00:00:00 51
6: 2018-08-13 00:15:00 55
7: 2018-08-13 00:30:00 57
8: 2018-08-13 00:45:00 60
9: 2018-08-13 01:00:00 61
10: 2018-08-13 01:15:00 62
Here, we have an unfilled gap in the first row. This is caused by the way the sequence of intervals is contructed. It can be avoided be a slight modification
df0[.(seq(ceiling_date(min(Timestamp), step), ceiling_date(max(Timestamp), step),by = step)),
on = .(Timestamp = V1), roll = TRUE]
Timestamp Value..kW.
1: 2018-08-12 23:15:00 51
2: 2018-08-12 23:30:00 51
3: 2018-08-12 23:45:00 51
4: 2018-08-13 00:00:00 51
5: 2018-08-13 00:15:00 55
6: 2018-08-13 00:30:00 57
7: 2018-08-13 00:45:00 60
8: 2018-08-13 01:00:00 61
9: 2018-08-13 01:15:00 62
Data
Th OP has provided data as dput()
df <-
structure(list(Timestamp = c("8/12/2018 23:00:00", "8/13/2018 0:00:00",
"8/13/2018 0:10:00", "8/13/2018 0:14:00", "8/13/2018 0:15:00",
"8/13/2018 0:19:00", "8/13/2018 0:29:00", "8/13/2018 0:38:00",
"8/13/2018 0:44:00", "8/13/2018 0:45:00", "8/13/2018 0:58:00",
"8/13/2018 1:01:00"), Value..kW. = 51:62), .Names = c("Timestamp",
"Value..kW."), class = "data.frame", row.names = c(NA, -12L))
EDIT: The OP has provided two slightly different datasets:
- as
dput()
with seconds truncated (df
in this answer)
- as printed
df
in the question witout truncated seconds (df0
in this answer)
This subtle difference impacts the results. So, here is the dataset as printed:
df0 <- data.frame(
readr::read_table(" Timestamp Value.(kW)
8/12/2018 23:00:06 51
8/13/2018 0:00:16 52
8/13/2018 0:10:26 53
8/13/2018 0:14:36 54
8/13/2018 0:15:00 55
8/13/2018 0:19:57 56
8/13/2018 0:29:09 57
8/13/2018 0:38:17 58
8/13/2018 0:44:59 59
8/13/2018 0:45:00 60
8/13/2018 0:58:47 61
8/13/2018 1:01:57 62
"))
# prepare
library(lubridate)
library(data.table)
setDT(df0)[, Timestamp := mdy_hms(Timestamp)]
8/12/2018 23:15:00
and8/13/2018 01:15:00
get values? Sounds like you have an implicit interval in mind for which you want the values, but you don't mention it. – Mikko Marttila Aug 20 '18 at 20:32structure(...)
do not match the printeddf
. Instructure(..)
the seconds have been truncated. – Uwe Aug 21 '18 at 8:14