假设这是dataframe(dt):
datetime price1 price2
2011-01-04 22:00:20 1 7
2011-01-04 22:01:37 2 8
2011-01-04 22:01:57 3 9
2011-01-04 22:03:03 4 10
2011-01-04 22:03:32 5 11
2011-01-04 22:03:45 6 12
我想按日期时间(间隔1分钟)拆分数据并找到:
对于price1:最后一次观察减去第一次。
对于price2:所有价格的总和。
time difference sum
2011-01-04 22:00:00 1-1=0 7
2011-01-04 22:01:00 3-2=1 17
2011-01-04 22:03:00 6-4=2 33
dt$time <- cut(dt$datetime, "1 min")
dt1min<-ddply(dt,.(time), function(x))
我应该如何定义函数(x)?感谢。
答案 0 :(得分:1)
以下是使用data.table
包的解决方案:
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]
输出是:
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