R:如何在重叠的时间段内求平均值 [英] R: how to average within overlapping time periods

查看:29
本文介绍了R:如何在重叠的时间段内求平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了一个类似的问题 here,不过这有点过于简单化了.所以我们又来了:

I have recently posted a similar question here, which was a bit oversimplified, though. Thus here we go again:

假设我有一个数据帧(下面的 dput 输出),其中包含许多不同变量的时间序列数据(本例中为 5,实际数据中更多):

Assume I have a dataframe (dput output below) with time series data of many different variables (5 in this example, many more in the real data):

          date          a  b  c  d  e
1  2009-10-01 00:00:00 10 20 30 40 50
2  2009-10-01 01:00:00 11 21 31 41 51
3  2009-10-01 02:00:00 12 22 32 42 52
4  2009-10-01 03:00:00 13 23 33 43 53
5  2009-10-01 04:00:00 14 24 34 44 54
6  2009-10-01 05:00:00 15 25 35 45 55
7  2009-10-01 06:00:00 16 26 36 46 56
8  2009-10-01 07:00:00 17 27 37 47 57
9  2009-10-01 08:00:00 18 28 38 48 58
10 2009-10-01 09:00:00 19 29 39 49 59
11 2009-10-01 10:00:00 20 30 40 50 60
12 2009-10-01 11:00:00 21 31 41 51 61
13 2009-10-01 12:00:00 22 32 42 52 62
14 2009-10-01 13:00:00 23 33 43 53 63
15 2009-10-01 14:00:00 24 34 44 54 64
16 2009-10-01 15:00:00 25 35 45 55 65
17 2009-10-01 16:00:00 26 36 46 56 66
18 2009-10-01 17:00:00 27 37 47 57 67
19 2009-10-01 18:00:00 28 38 48 58 68
20 2009-10-01 19:00:00 29 39 49 59 69
21 2009-10-01 20:00:00 30 40 50 60 70
22 2009-10-01 21:00:00 31 41 51 61 71
23 2009-10-01 22:00:00 32 42 52 62 72
24 2009-10-01 23:00:00 33 43 53 63 73
25 2009-10-02 00:00:00 34 44 54 64 74

和另一个数据框事件",具有由开始和停止日期定义的不同时间段(此处为 3 个,实际数据中还有更多):

and another data frame "events" with different time periods defined by a start and stop date (3 here, many more in the real data):

   id       start                stop
1 AGH 2009-10-01 02:00:00 2009-10-01 04:00:00
2 TRG 2009-10-01 03:00:00 2009-10-01 10:00:00
3 ZUH 2009-10-01 03:00:00 2009-10-01 20:00:00

我想得到一个不同事件中变量平均值的表格,如下所示:

I would like to get a table of the mean values of the variables within the different events like this:

   id avg(y.a) avg(y.b) avg(y.c) avg(y.d) avg(y.e)
1 AGH     13.0     23.0     33.0     43.0     53.0
2 TRG     16.5     26.5     36.5     46.5     56.5
3 ZUH     21.5     31.5     41.5     51.5     61.5

我从上一篇文章中了解到,我可以使用 sqldf 包和一个相当简单的 SQL 语句来做到这一点:

I have learned from my previous post that I can do this using the sqldf package and a rather simple SQL statement:

means <- sqldf("
+     SELECT x.id, avg(y.a), avg(y.b), avg(y.c), avg(y.d), avg(y.e) 
+     FROM events as x, data as y 
+     WHERE y.date between x.start and x.stop 
+     GROUP BY x.id 
+ ")

然而,由于实际数据包含更多要平均的列,这些列在我必须处理的各种文件中的名称不同,将所有列名输入到 SQL 语句中变得有点乏味.

However, as the real data contains many more columns to average, which are named differently in the various files I have to process, typing all the column names into the SQL statements becomes a bit tedious.

因此,我更喜欢 R 中的解决方案,在那里我可以简单地按列的编号 (data[2:100]) 引用列,但困难在于,时间段不连续且重叠,而 id是字符串.

Thus I would prefer a solution in R, where I can simply refer to the columns by their number (data[2:100]) The difficulty is, though, that the time periods are non-continous and overlapping and the ids are character strings.

任何想法如何做到这一点将不胜感激!

Any ideas how to do this would be much appreciated!

dput(数据)

structure(list(date = structure(c(1254348000, 1254351600, 1254355200, 
1254358800, 1254362400, 1254366000, 1254369600, 1254373200, 1254376800, 
1254380400, 1254384000, 1254387600, 1254391200, 1254394800, 1254398400, 
1254402000, 1254405600, 1254409200, 1254412800, 1254416400, 1254420000, 
1254423600, 1254427200, 1254430800, 1254434400), class = c("POSIXct", 
"POSIXt"), tzone = "Europe/Berlin"), a = 10:34, b = 20:44, c = 30:54, 
    d = 40:64, e = 50:74), .Names = c("date", "a", "b", "c", 
"d", "e"), row.names = c(NA, -25L), class = "data.frame")

dput(事件)

structure(list(id = structure(1:3, .Label = c("AGH", "TRG", "ZUH"
), class = "factor"), start = structure(c(1254355200, 1254358800, 
1254358800), class = c("POSIXct", "POSIXt"), tzone = "Europe/Berlin"), 
    stop = structure(c(1254362400, 1254384000, 1254420000), class = c("POSIXct", 
    "POSIXt"), tzone = "Europe/Berlin")), .Names = c("id", "start", 
"stop"), row.names = c(NA, -3L), class = "data.frame")

推荐答案

  1. 基本问题是数据没有归一化;然而,我们可以动态生成 sql 语句:

  1. The basic problem is due to the fact that the data is not normalized; however, short of putting it into long form we could dynamically generate the sql statement:

library(sqldf)
sql <- paste("select id, ", 
    toString(sprintf("avg(y.%s)", names(data)[-1])),
    "from events as x, data as y
    where y.date between x.start and x.stop
    group by x.id")
sqldf(sql)

  • 作为替代,我们展示了使用 reshape2 包中的 melt 将数据转换为长格式,data_long,对其进行处理以给出 means.long 并使用 dcast 将其转换回宽格式:

  • As an alternative, we show the use of melt in the reshape2 package to convert the data to long form, data_long, process it to give means.long and convert it back to wide form using dcast :

    library(reshape2)
    data_long <- melt(data, id.vars = "date")
    means_long <- sqldf("
         SELECT x.id, y.variable, avg(value)
         FROM events as x, data_long as y 
         WHERE y.date between x.start and x.stop 
         GROUP BY x.id, y.variable
    ")
    means <- dcast(id ~ variable, data = means_long, value.var = "avg(value)")
    

  • 这篇关于R:如何在重叠的时间段内求平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆