如何将每周数据转换为月度数据?

时间:2016-03-13 17:31:06

标签: r

例如,这是我的数据。 我想通过加权平均值将每周数据转换为月度数据。事实上,7天X1数据中的一些将从一个月到另一个重叠。

    Week                    interest Start        End
1   2004-01-04 - 2004-01-10       62 2004-01-04 2004-01-10
2   2004-01-11 - 2004-01-17       70 2004-01-11 2004-01-17
3   2004-01-18 - 2004-01-24       69 2004-01-18 2004-01-24
4   2004-01-25 - 2004-01-31       68 2004-01-25 2004-01-31
5   2004-02-01 - 2004-02-07       63 2004-02-01 2004-02-07
6   2004-02-08 - 2004-02-14       65 2004-02-08 2004-02-14
7   2004-02-15 - 2004-02-21       66 2004-02-15 2004-02-21
8   2004-02-22 - 2004-02-28       66 2004-02-22 2004-02-28
9   2004-02-29 - 2004-03-06       70 2004-02-29 2004-03-06
10  2004-03-07 - 2004-03-13       80 2004-03-07 2004-03-13
11  2004-03-14 - 2004-03-20       82 2004-03-14 2004-03-20

我尝试将这些数据转换为每日数据,然后将每日数据汇总到每月数据中,但我不知道如何转换它。更具体地说,数据框如下所示:

2004-01-04 62 
2004-01-05 62 
2004-01-06 62 
... 
2004-01-10 62 
2005-01-11 70 
.... 
... 

1 个答案:

答案 0 :(得分:2)

将数据读入动物园对象,将其与每日网格合并,并使用void SVC_Handler(void) { // Get stack pointer, assuming we the thread that generated // the svc call was using the psp stack instead of msp unsigned int *stack; asm volatile ("MRS %0, psp\n\t" : "=rm" (stack) ); // Stack frame contains: // r0, r1, r2, r3, r12, r14, the return address and xPSR // - Stacked R0 = stack[0] // - Stacked R1 = stack[1] // - Stacked R2 = stack[2] // - Stacked R3 = stack[3] // - Stacked R12 = stack[4] // - Stacked LR = stack[5] // - Stacked PC = stack[6] // - Stacked xPSR= stack[7] // Thumb instructions have 2 bytes instead of 4, then get // the first byte of the instruction right before the // instruction pointed by the stacked PC unsigned int svc_number = ((char *)svc_args[6])[-2]; switch(svc_number) { case 0: ... break; case 1: ... break; } 填充空白日期。然后按年/月汇总:

na.locf

,并提供:

# input data frame in reproducible form

Lines <- "interest Start        End
62 2004-01-04 2004-01-10
70 2004-01-11 2004-01-17
69 2004-01-18 2004-01-24
68 2004-01-25 2004-01-31
63 2004-02-01 2004-02-07
65 2004-02-08 2004-02-14
66 2004-02-15 2004-02-21
66 2004-02-22 2004-02-28
70 2004-02-29 2004-03-06
80 2004-03-07 2004-03-13
82 2004-03-14 2004-03-20"
DF <- read.table(text = Lines, header = TRUE)

# convert to zoo, merge with a daily grid and aggregate by month using mean

library(zoo)
z.st <- read.zoo(DF[c("Start", "interest")])
z.en <- read.zoo(DF[c("End", "interest")])
z <- c(z.st, z.en)

g <- zoo(, seq(start(z), end(z), "day"))
m <- na.locf(merge(z, g))
aggregate(m, as.yearmon, mean)

更新:已修复