将函数应用于R中的每小时数据

时间:2015-04-20 08:31:15

标签: r statistics

我在HISTORY表中有以下数据,列名为:

ID, START_TIME, END_TIME, VALUE

51,2015-04-17 01:00:00,2015-04-17 01:10:00,98
51,2015-04-17 01:10:00,2015-04-17 01:20:00,96
51,2015-04-17 01:20:00,2015-04-17 01:30:00,97
51,2015-04-17 01:30:00,2015-04-17 01:40:00,99
51,2015-04-17 01:40:00,2015-04-17 01:50:00,98
51,2015-04-17 01:50:00,2015-04-17 02:00:00,105
51,2015-04-17 02:00:00,2015-04-17 02:10:00,103
51,2015-04-17 02:10:00,2015-04-17 02:20:00,101
51,2015-04-17 02:20:00,2015-04-17 02:30:00,100
51,2015-04-17 02:30:00,2015-04-17 02:40:00,104
51,2015-04-17 02:40:00,2015-04-17 02:50:00,102
51,2015-04-17 02:50:00,2015-04-17 03:00:00,98
51,2015-04-17 03:00:00,2015-04-17 03:10:00,97
51,2015-04-17 03:10:00,2015-04-17 03:20:00,96
51,2015-04-17 03:20:00,2015-04-17 03:30:00,99
51,2015-04-17 03:30:00,2015-04-17 03:40:00,100
51,2015-04-17 03:40:00,2015-04-17 03:50:00,101
51,2015-04-17 03:50:00,2015-04-17 04:00:00,102
51,2015-04-17 04:00:00,2015-04-17 04:10:00,99
51,2015-04-17 04:10:00,2015-04-17 04:20:00,104
51,2015-04-17 04:20:00,2015-04-17 04:30:00,105
51,2015-04-17 04:30:00,2015-04-17 04:40:00,103
51,2015-04-17 04:40:00,2015-04-17 04:50:00,98
51,2015-04-17 04:50:00,2015-04-17 05:00:00,97
51,2015-04-17 05:00:00,2015-04-17 05:10:00,101
51,2015-04-17 05:10:00,2015-04-17 05:20:00,103
51,2015-04-17 05:20:00,2015-04-17 05:30:00,101
51,2015-04-17 05:30:00,2015-04-17 05:40:00,105
51,2015-04-17 05:40:00,2015-04-17 05:50:00,102
51,2015-04-17 05:50:00,2015-04-17 06:00:00,98

我想将像max()这样的函数应用于VALUE列但是有一些频率。如果频率假设为1小时,那么对于该数据,将对5个不同的组应用最大函数。

实施例。从2015-04-17 01:00:00开始到2015-04-17 02:00:00等。 如何在r中实现这一点。最终输出将是这样的:

51, 2015-04-17 02:00:00, 105
51, 2015-04-17 03:00:00, 102
51, 2015-04-17 04:00:00, 104
51, 2015-04-17 05:00:00, 105
51, 2015-04-17 06:00:00, 105

其中上面的列是ID,START_TIME是计算max()的值,该值是该小时的max()函数的结果。 如何在r中实现这一点。使用间隔或其他东西?

谢谢..

3 个答案:

答案 0 :(得分:4)

以下是使用data.table

的另一种方式
library(data.table)
setDT(df)[, .(MAX_VALUE = max(VALUE)), 
             by = .(ID, START_TIME = as.POSIXct(START_TIME, format = "%F %H") + 3600)]
#    ID          START_TIME MAX_VALUE
# 1: 51 2015-04-17 02:00:00       105
# 2: 51 2015-04-17 03:00:00       104
# 3: 51 2015-04-17 04:00:00       102
# 4: 51 2015-04-17 05:00:00       105
# 5: 51 2015-04-17 06:00:00       105

或没有任何包依赖的类似解决方案

df$START_TIME2 <-  as.POSIXct(df$START_TIME, format = "%F %H") + 3600
aggregate(VALUE ~ ID + START_TIME2, df, max)
#   ID         START_TIME2 VALUE
# 1 51 2015-04-17 02:00:00   105
# 2 51 2015-04-17 03:00:00   104
# 3 51 2015-04-17 04:00:00   102
# 4 51 2015-04-17 05:00:00   105
# 5 51 2015-04-17 06:00:00   105

答案 1 :(得分:3)

你可以尝试

library(dplyr)
HISTORY %>% 
  group_by(ID, TIME = format(START_TIME + 60*60, "%Y-%m-%d %H:00:00")) %>% 
  summarise(MAX_VALUE = max(VALUE))
#   ID                TIME MAX_VALUE
# 1 51 2015-04-17 02:00:00       105
# 2 51 2015-04-17 03:00:00       104
# 3 51 2015-04-17 04:00:00       102
# 4 51 2015-04-17 05:00:00       105
# 5 51 2015-04-17 06:00:00       105

答案 2 :(得分:1)

以下是使用data.table

的可能解决方案
library(data.table)
setDT(df)[, max(VALUE), by = .(START_TIME = sub(":.*", "", START_TIME))]
     START_TIME  V1
1: 2015-04-17 01 105
2: 2015-04-17 02 104
3: 2015-04-17 03 102
4: 2015-04-17 04 105
5: 2015-04-17 05 105