【发布时间】:2015-12-09 22:04:04
【问题描述】:
我有一个包含时间数据的数据库,但对于某些时间戳,没有可用的数据(数据库中的 NA)。我想对这些值进行插值。
数据集:
structure(list(timestamp = structure(1:7, .Label = c("21/01/2012 18:41",
+ "21/01/2012 18:46", "21/01/2012 18:51", "21/01/2012 18:56", "21/01/2012 19:01",
+ "21/01/2012 19:06", "21/01/2012 19:11"), class = "factor"), humid = c(47.7,
+ 44.5, NA, 42.5, 42.5, NA, 41.6), temp = c(14.12, 15.37, NA, 16.17,
+ 16.31, NA, 16.51)), .Names = c("timestamp", "humid", "temp"), class = "data.frame", row.names = c(NA,
+ -7L))
看起来像这样:
timestamp humid temp
1 21/01/2012 18:41 47.700000000000003 14.119999999999999
2 21/01/2012 18:46 44.500000000000000 15.369999999999999
3 21/01/2012 18:51 NA NA
4 21/01/2012 18:56 42.500000000000000 16.170000000000002
5 21/01/2012 19:01 42.500000000000000 16.309999999999999
6 21/01/2012 19:06 NA NA
7 21/01/2012 19:11 41.600000000000001 16.510000000000002
我已经尝试过选项 A:
library(zoo)
Mz <- zoo(TEST)
index(Mz) <- Mz[,1]
Mz_approx <- na.approx(Mz, x=Mz$timestamp)
但这会导致以下错误:
Error in approx(x[!na], y[!na], xout, ...) :
need at least two non-NA values to interpolate
In addition: Warning messages:
1: In na.approx.default(object, x = x, xout = xout, na.rm = FALSE, :
NAs introduced by coercion
2: In na.approx.default(object, x = x, xout = xout, na.rm = FALSE, :
NAs introduced by coercion
3: In xy.coords(x, y) : NAs introduced by coercion
我也尝试了选项 B:
library(zoo)
Mz <- zoo(TEST)
Mz_approx <- na.approx(Mz)
但这会导致以下错误:
Error in approx(x[!na], y[!na], xout, ...) :
need at least two non-NA values to interpolate
In addition: Warning message:
In xy.coords(x, y) : NAs introduced by coercion
克服这些错误并正确使用函数 na.approx 的最佳方法是什么?
【问题讨论】:
-
library("imputeTS") na.interpolation(yourList) - 将一些作为 na.approx 并且您不必转换为之前的动物园系列
标签: r interpolation zoo