Here is my solution using the data.table
package:
library(data.table)
dt <- data.table(datetime= c("2011-01-04 22:00:20",
"2011-01-04 22:01:37",
"2011-01-04 22:01:57",
"2011-01-04 22:03:03",
"2011-01-04 22:03:32",
"2011-01-04 22:03:45"),
price1 = c(1,2,3,4,5,6),
price2 = c(7,8,9,10,11,12))
dt[, datetime:= as.POSIXct(datetime)] # convert character to timestamp
dt[, time:= format(datetime, "%Y-%m-%d %H:%M")] # create time column
# now split apply,combine
dt[, list(difference = price1[datetime==max(datetime)] - price1[datetime==min(datetime)],
sum = sum(price2)),
by = time]
the output is:
time difference sum
1: 2011-01-04 22:00 0 7
2: 2011-01-04 22:01 1 17
3: 2011-01-04 22:03 2 33