【问题标题】:merging aggregate data in R (again)合并 R 中的聚合数据(再次)
【发布时间】:2013-07-24 17:43:20
【问题描述】:

跟进我之前的question with the same title,我有一个长期的次小时数据,我想以各种方式汇总数据。我想根据一天中的时间进行聚合,但也需要聚合组合,例如,day-type-hourly(即周日凌晨 1 点、周日凌晨 2 点等)。另一个例子是:周末或工作日每小时。

下面的示例显示了我所做的两种聚合。我已经做到了。所以我最终得到了两个动物园对象。我接下来要做的是将聚合合并到原始数据中,这样我就可以比较聚合的错误。这就是我目前卡住的地方。

请注意,我不使用the previous question 中的解决方案,因为我想要聚合的灵活性。

这是显示我到目前为止尝试过的 sn-p。任何帮助将不胜感激。

library(zoo)
Lines <- "Index,light.kw
2013-06-14 13:00:00,3.436
2013-06-14 13:15:00,3.327
2013-06-14 13:30:00,3.319
2013-06-14 13:45:00,3.308
2013-06-14 14:00:00,3.458
2013-06-14 14:15:00,3.452
2013-06-14 14:30:00,3.445
2013-06-14 14:45:00,3.469
2013-06-14 15:00:00,3.468
2013-06-14 15:15:00,3.427
2013-06-14 15:30:00,3.168
2013-06-14 15:45:00,2.383
2013-06-15 13:00:00,0.555
2013-06-15 13:15:00,0.555
2013-06-15 13:30:00,0.555
2013-06-15 13:45:00,0.555
2013-06-15 14:00:00,0.555
2013-06-15 14:15:00,0.555
2013-06-15 14:30:00,0.555
2013-06-15 14:45:00,0.719
2013-06-15 15:00:00,0.976
2013-06-15 15:15:00,0.981
2013-06-15 15:30:00,1.116
2013-06-15 15:45:00,0.59"
con <- textConnection(Lines)
z <- read.zoo(con, header=TRUE, sep=",",
     format="%Y-%m-%d %H:%M:%S", FUN=as.POSIXct)
close(con)

index.hourly = format(index(z), "%H")
z.hourly = aggregate(z, index.hourly, mean)
z.hourly
merge(z,z.hourly)

index.dayhour = format(index(z), "%w %H")
z.dayhour = aggregate(z, index.dayhour, mean)
z.dayhour
merge(z,z.dayhour)

【问题讨论】:

  • 如何通过与原始数据合并来控制聚合误差?
  • 您可能希望这样的代码能够工作,但我怀疑它可能需要构建一个中间列:merge(z,z.hourly, by.x=format(index(z),"%H") )merge(z,z.dayhour, by.x=format(index(z), "%w %H") )
  • @agstudy 不是为了“控制错误”,我只是想在原始数据和聚合之间进行并排比较,这样我就可以使用聚合作为原始数据。
  • @DWin 谢谢,我根据这个建议开发了一个解决方案。

标签: r merge aggregate zoo


【解决方案1】:

根据上面的DWin's 建议,这是我找到的解决方案。请注意,DWin 建议的与中间列合并在 zoo 中不起作用,因此此解决方案涉及将 zoo 对象转换回数据帧并将合并作为数据帧。这里是:

library(zoo)
Lines <- "Index,light.kw
2013-06-14 13:00:00,3.436
2013-06-14 13:15:00,3.327
2013-06-14 13:30:00,3.319
2013-06-14 13:45:00,3.308
2013-06-14 14:00:00,3.458
2013-06-14 14:15:00,3.452
2013-06-14 14:30:00,3.445
2013-06-14 14:45:00,3.469
2013-06-14 15:00:00,3.468
2013-06-14 15:15:00,3.427
2013-06-14 15:30:00,3.168
2013-06-14 15:45:00,2.383
2013-06-15 13:00:00,0.555
2013-06-15 13:15:00,0.555
2013-06-15 13:30:00,0.555
2013-06-15 13:45:00,0.555
2013-06-15 14:00:00,0.555
2013-06-15 14:15:00,0.555
2013-06-15 14:30:00,0.555
2013-06-15 14:45:00,0.719
2013-06-15 15:00:00,0.976
2013-06-15 15:15:00,0.981
2013-06-15 15:30:00,1.116
2013-06-15 15:45:00,0.59"
con <- textConnection(Lines)
z <- read.zoo(con, header=TRUE, sep=",",
     format="%Y-%m-%d %H:%M:%S", FUN=as.POSIXct)
close(con)

# make the index for aggregation
index.hourly <- format(index(z), "%H")
# make the aggregate
z.hourly = aggregate(z, index.hourly, mean, na.rm=T)

# make a data frame from the original zoo,
# but the data frame must include the index.hourly
# so that later we can merge the data frame based
# on this index.
# First, make a zoo object of the index and then
# merge this with the original zoo.
z.index.hourly = zoo(index.hourly,index(z))
z.with.index = merge(z,z.index.hourly)
# make a dataframe of the last zoo
df1 = as.data.frame(z.with.index)
# add the index of the df1 (which is the timestamp) as a column
# as we will need the timestamp to rebuild the zoo object.
df1$Index = row.names(df1)

# make a dataframe of the aggregate zoo
df2 = as.data.frame(z.hourly)
df2$Index = row.names(df2)

# merge the two data frame
df3 = merge(df1,df2,by.x="z.index.hourly",by.y="Index",all.x=T)
df3 = df3[order(df3$Index),]
summary(df3)

# make a zoo object containing the original data and the aggregate
z.merged.agg = zoo(df3[,c(2,4)],as.POSIXct(df3$Index, tz="GMT"))
z.merged.agg

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 2018-07-05
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2015-06-28
    • 2016-01-13
    • 2015-08-12
    • 2020-08-16
    相关资源
    最近更新 更多