create an index for aggregating daily data to match periodic data

◇◆丶佛笑我妖孽 提交于 2019-12-25 01:09:37

问题


I have daily measurements prec.d and periodic measurements prec.p. The periodic measurements (3-12 days apart) are roughly the sum of the daily measurements between the start and end dates, and I need to compare prec in the two data frames. I have so far manually created an index week that represents the time span of each periodic measurement, but it would be great to make week in a reproducible fashion.

data.frame prec.d

     day  week  prec
6/20/2013   1   0
6/21/2013   1   0
6/22/2013   1   0
6/23/2013   1   0
6/24/2013   1   41.402
6/25/2013   1   2.794
6/26/2013   1   6.096
6/27/2013   2   0.508
6/28/2013   2   0
6/29/2013   2   0
6/30/2013   2   2.54
7/1/2013    2   18.034
7/2/2013    2   4.064

And data.frame prec.p

  start         end   week  prec1      prec2        prec3
6/20/2013   6/26/2013   1   50.28   31.78042615 42.76461716
6/27/2013   7/2/2013    2   25.1    15.70964247 20.49507586

I would like to create the week field automatically, which spans from start to end in prec.p. The I can aggregate by week to make prec in both data frames match.


回答1:


Introduce YYYYWW field in both weekly and minute data, WW stands for week number, that will give you a common index. For example

x <- as.Date(runif(100)*100)
yyyyww <- strftime(x, format="%Y%U")
yyyyww 

Or take a look at package quantmod, if I remember correctly, it has functions for time frame conversion.



来源:https://stackoverflow.com/questions/20231475/create-an-index-for-aggregating-daily-data-to-match-periodic-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!