如何按日期将数据集转换为xts,但我还想删除包含NA值的日期,如下所示:data set contain NA 我已经执行转换为xts,使用以下代码:
tlkm_ts <- xts(tlkm[,-1], order.by = as.Date(tlkm[,1], "%Y-%m-%d"), na.rm = TRUE)
但导致不删除包含NA值的日期。我想使用xts数据集来执行时间序列预测,有人可以帮助我吗?
答案 0 :(得分:2)
了解对象tlkm
的类型有点困难,但让我们说它是一个矩阵
col1 <- c("2017-12-06", "2017-12-07", "2017-12-08", "2017-12-09", "2017-12-10",
"2017-12-11", "2017-12-12", "2017-12-13", "2017-12-14", "2017-12-15",
"2017-12-16", "2017-12-17", "2017-12-18", "2017-12-19", "2017-12-20",
"2017-12-21", "2017-12-22", "2017-12-27", "2017-12-28", "2017-12-29",
"2017-12-30", "2017-12-31", "2018-01-01")
col2 <- c(4200, 4200, 4140, NA, NA, 4140, 4170, 4200, 4250, 4230, NA, NA, 4240,
4190, 4160, 4250, 4300, 4300, 4390, 4440, NA, NA, NA)
tlkm <- matrix(c(col1, col2), ncol = 2)
然后你可能想在na.exclude()
函数中使用na.rm=TRUE
而不是参数xts()
tlkm_ts <- na.exclude(xts(as.numeric(tlkm[, -1]), order.by = as.Date(tlkm[, 1])))
给出了
> tlkm_ts
[,1]
2017-12-06 4200
2017-12-07 4200
2017-12-08 4140
2017-12-11 4140
2017-12-12 4170
2017-12-13 4200
2017-12-14 4250
2017-12-15 4230
2017-12-18 4240
2017-12-19 4190
2017-12-20 4160
2017-12-21 4250
2017-12-22 4300
2017-12-27 4300
2017-12-28 4390
2017-12-29 4440
希望有所帮助