data.table
makes this convenient. Here, columns are added for the first and last value in each minute:
xt <- data.table(x)
xt[,first:=head(.SD,1), by=time %/% 60]
xt[,last:=tail(.SD,1), by=time %/% 60]
xt
## time value first last
## 1: 53 5 5 7
## 2: 55 5 5 7
## 3: 59 7 5 7
## 4: 61 9 9 11
## 5: 79 6 9 11
## 6: 118 11 9 11
## 7: 200 5 5 5
Here is one easy way to trim this to the minute buckets. Modify the time
column so that it indicates the head of the minute, remove the value
column, and pass to unique
:
xt$time <- 60 * xt$time %/% 60
xt$value <- NULL
unique(xt)
## time first last
## 1: 0 5 7
## 2: 60 9 11
## 3: 180 5 5
To get the times and values for the first and last rows in each minute, aggregate.data.frame
works well, but you need two passes.
First values:
aggregate(cbind(time, value) ~ time %/% 60, data=x, FUN=head, 1)
## time%/%60 time value
## 1 0 53 5
## 2 1 61 9
## 3 3 200 5
Last values:
aggregate(cbind(time, value) ~ time %/% 60, data=x, FUN=tail, 1)
## time%/%60 time value
## 1 0 59 7
## 2 1 118 11
## 3 3 200 5
These may then be combined into the desired output.