假设这是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)?谢谢。
发布于 2014-04-21 20:38:42
下面是我使用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 33https://stackoverflow.com/questions/23204581
复制相似问题