我想按时间间隔聚合数据框,对每一列应用不同的函数。我想我几乎已经aggregate
失败了,并且已经将我的数据分成了chron
包的间隔,这很容易。
但我不确定如何处理子集。所有的映射函数 , 都*apply
采用*ply
一个函数(我希望能够采用函数向量来应用每列或变量,但没有找到)所以我正在编写一个函数来获取我的数据帧子集,并给我所有变量的平均值,除了“时间”,它是索引,和“径流”,它应该是总和。
我试过这个:
aggregate(d., list(Time=trunc(d.$time, "00:10:00")), function (dat) with(dat,
list(Time=time[1], mean(Port.1), mean(Port.1.1), mean(Port.2), mean(Port.2.1),
mean(Port.3), mean(Port.3.1), mean(Port.4), mean(Port.4.1), Runoff=sum(Port.5))))
即使它没有给我这个错误,这也会很丑陋:
Error in eval(substitute(expr), data, enclos = parent.frame()) :
not that many frames on the stack
这告诉我我真的做错了什么。根据我对 RI 的了解,我认为必须有一种优雅的方式来做到这一点,但它是什么?
输入:
d. <- structure(list(time = structure(c(15030.5520833333, 15030.5555555556,
15030.5590277778, 15030.5625, 15030.5659722222), format = structure(c("m/d/y",
"h:m:s"), .Names = c("dates", "times")), origin = structure(c(1,
1, 1970), .Names = c("month", "day", "year")), class = c("chron",
"dates", "times")), Port.1 = c(0.359747, 0.418139, 0.417459,
0.418139, 0.417459), Port.1.1 = c(1.3, 11.8, 11.9, 12, 12.1),
Port.2 = c(0.288837, 0.335544, 0.335544, 0.335544, 0.335544
), Port.2.1 = c(2.3, 13, 13.2, 13.3, 13.4), Port.3 = c(0.253942,
0.358257, 0.358257, 0.358257, 0.359002), Port.3.1 = c(2,
12.6, 12.7, 12.9, 13.1), Port.4 = c(0.352269, 0.410609, 0.410609,
0.410609, 0.410609), Port.4.1 = c(5.9, 17.5, 17.6, 17.7,
17.9), Port.5 = c(0L, 0L, 0L, 0L, 0L)), .Names = c("time",
"Port.1", "Port.1.1", "Port.2", "Port.2.1", "Port.3", "Port.3.1",
"Port.4", "Port.4.1", "Port.5"), row.names = c(NA, 5L), class = "data.frame")