如何正确地每周转换为更短的跨度数据?

时间:2016-10-18 10:55:18

标签: r interpolation

我有一个包含每周时间序列数据的数据框。 我需要用线性近似的每日插值来转换它。

DATE        VALUE1   VALUE2 VALUE3  VALUE4  VALUE5  VALUE6  VALUE7  VALUE8  VALUE9  VALUE10
02-01-2014  95.58   -22.43  73.16   0.09    0.3     1.53    7.14    67.17   7.33    74.5
09-01-2014  113.65  -41     72.65   0.07    0.65    1.77    8.14    65.56   7.47    73.02
16-01-2014  85.87   -15.29  70.59   0.13    0.18    1.78    6.19    65.88   7.5     73.38

我需要有

的值
02-01-2014
03-01-2014
04-01-2014
05-01-2014
06-01-2014
07-01-2014
08-01-2014
09-01-2014
10-01-2014

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

使用readr包读取数据:

df <- readr::read_table("DATE        VALUE1   VALUE2 VALUE3  VALUE4  VALUE5  VALUE6  VALUE7  VALUE8  VALUE9  VALUE10
02-01-2014  95.58   -22.43  73.16   0.09    0.3     1.53    7.14    67.17   7.33    74.5
09-01-2014  113.65  -41     72.65   0.07    0.65    1.77    8.14    65.56   7.47    73.02
16-01-2014  85.87   -15.29  70.59   0.13    0.18    1.78    6.19    65.88   7.5     73.38")

生成输出时间并解析数据中的日期:

days = as.Date("2014-01-02") + 0:14
week_dates = as.Date(df$DATE, format = "%d-%m-%Y")

最后,线性近似:

f <- function(column) {
  approx(week_dates, column, xout = days)$y
}
result <- sapply(df[-1], f)
rownames(result) <- as.character(days)

答案 1 :(得分:0)

您可以使用na.approx库中的zoo函数,通过线性插值将每周频率转换为每日频率     中间值

调整?zoo页面中的示例:

library("zoo")

weeklyDF = read.table(text="DATE        VALUE1   VALUE2 VALUE3  VALUE4  VALUE5  VALUE6  VALUE7  VALUE8  VALUE9  VALUE10
02-01-2014  95.58   -22.43  73.16   0.09    0.3     1.53    7.14    67.17   7.33    74.5
09-01-2014  113.65  -41     72.65   0.07    0.65    1.77    8.14    65.56   7.47    73.02
16-01-2014  85.87   -15.29  70.59   0.13    0.18    1.78    6.19    65.88   7.5     73.38",header=TRUE,stringsAsFactors=FALSE)


weeklyDF$DATE = as.Date(weekDF$DATE,format="%d-%m-%Y")

weeklyZoo = zoo(x=weeklyDF[,-1],order.by=weeklyDF[,1])

weeklyZoo
#           VALUE1 VALUE2 VALUE3 VALUE4 VALUE5 VALUE6 VALUE7 VALUE8 VALUE9 VALUE10
#2014-01-02  95.58 -22.43  73.16   0.09   0.30   1.53   7.14  67.17   7.33   74.50
#2014-01-09 113.65 -41.00  72.65   0.07   0.65   1.77   8.14  65.56   7.47   73.02
#2014-01-16  85.87 -15.29  70.59   0.13   0.18   1.78   6.19  65.88   7.50   73.38



dailyZoo = na.approx(weeklyZoo, xout = seq(start(weeklyZoo), end(weeklyZoo), by = "day"))

dailyZoo
#              VALUE1    VALUE2   VALUE3     VALUE4    VALUE5   VALUE6   VALUE7   VALUE8   VALUE9  VALUE10
#2014-01-02  95.58000 -22.43000 73.16000 0.09000000 0.3000000 1.530000 7.140000 67.17000 7.330000 74.50000
#2014-01-03  98.16143 -25.08286 73.08714 0.08714286 0.3500000 1.564286 7.282857 66.94000 7.350000 74.28857
#2014-01-04 100.74286 -27.73571 73.01429 0.08428571 0.4000000 1.598571 7.425714 66.71000 7.370000 74.07714
#2014-01-05 103.32429 -30.38857 72.94143 0.08142857 0.4500000 1.632857 7.568571 66.48000 7.390000 73.86571
#2014-01-06 105.90571 -33.04143 72.86857 0.07857143 0.5000000 1.667143 7.711429 66.25000 7.410000 73.65429
#2014-01-07 108.48714 -35.69429 72.79571 0.07571429 0.5500000 1.701429 7.854286 66.02000 7.430000 73.44286
#2014-01-08 111.06857 -38.34714 72.72286 0.07285714 0.6000000 1.735714 7.997143 65.79000 7.450000 73.23143
#2014-01-09 113.65000 -41.00000 72.65000 0.07000000 0.6500000 1.770000 8.140000 65.56000 7.470000 73.02000
#2014-01-10 109.68143 -37.32714 72.35571 0.07857143 0.5828571 1.771429 7.861429 65.60571 7.474286 73.07143
#2014-01-11 105.71286 -33.65429 72.06143 0.08714286 0.5157143 1.772857 7.582857 65.65143 7.478571 73.12286
#2014-01-12 101.74429 -29.98143 71.76714 0.09571429 0.4485714 1.774286 7.304286 65.69714 7.482857 73.17429
#2014-01-13  97.77571 -26.30857 71.47286 0.10428571 0.3814286 1.775714 7.025714 65.74286 7.487143 73.22571
#2014-01-14  93.80714 -22.63571 71.17857 0.11285714 0.3142857 1.777143 6.747143 65.78857 7.491429 73.27714
#2014-01-15  89.83857 -18.96286 70.88429 0.12142857 0.2471429 1.778571 6.468571 65.83429 7.495714 73.32857
#2014-01-16  85.87000 -15.29000 70.59000 0.13000000 0.1800000 1.780000 6.190000 65.88000 7.500000 73.38000