R 发布使用数据框为缺失行添加 NA 值
R Issue adding NA values for missing rows using data frames
在此先感谢您提供的任何帮助。
长话短说:我正在处理来自测量设备的每小时时间序列数据(从 SQL 导出然后导入到 R 中以便正确格式化日期时间)- 时间序列包含缺失数据,有时成组,我需要找到这些丢失的 rows/indices 并为每个包含 NA
值的实例插入一个新行。
没有解决我问题的相关问题:
Adding row to a data frame with missing values
问题数据
我在这种情况下使用的数据集相当大,并且根据我 select 的测量设备而有所不同。作为测试用例,我有一个包含 17469 小时观察值的时间序列。我找到了一小部分可用于测试目的的数据集。这是:
> snip
date Reading
408 2015-12-15 00:00:00 4.40
409 2015-12-14 23:00:00 4.62
410 2015-12-14 22:00:00 4.61
411 2015-12-14 21:00:00 6.15
412 2015-12-14 20:00:00 6.06
413 2015-12-14 19:00:00 7.04
414 2015-12-14 18:00:00 8.57
415 2015-12-14 11:00:00 4.12
416 2015-12-14 10:00:00 3.73
我们可以看到缺少 2015-12-14 12:00:00 到 2015-12-14 17:00:00 的观测值。我想先定位然后用这些日期时间填充时间序列,并在这些位置的阅读列中输入 NA
。我还想 return 附加向量中缺少的索引。
如何做到这一点?
到目前为止,我已经尝试了以下代码(如此处所建议的那样,how to add a missing dates and remove repeated dates in hourly time series),但是当我执行 merge
函数时,我最终得到的只是 NA
值,并且仍然需要确定丢失的索引所在的位置。
结果如下:
> grid = data.frame(date=seq.POSIXt(min(snip[,1]), to=max(snip[,1]), by="1 hours"));
> dat = merge(grid, snip, by="date", all.x=TRUE)
> dat
date Reading
1 2015-12-14 10:00:00 NA
2 2015-12-14 11:00:00 NA
3 2015-12-14 12:00:00 NA
4 2015-12-14 13:00:00 NA
5 2015-12-14 14:00:00 NA
6 2015-12-14 15:00:00 NA
7 2015-12-14 16:00:00 NA
8 2015-12-14 17:00:00 NA
9 2015-12-14 18:00:00 NA
10 2015-12-14 19:00:00 NA
11 2015-12-14 20:00:00 NA
12 2015-12-14 21:00:00 NA
13 2015-12-14 22:00:00 NA
14 2015-12-14 23:00:00 NA
15 2015-12-15 00:00:00 NA
我在这里错过了什么?是不是因为grid
和snip$date
顺序颠倒了?有关更多信息,请查看日期时间格式(如果这是我的问题所在):
> snip[2,1]
[1] "2015-12-14 23:00:00 GMT"
dput(snip)命令的结果如下(感谢@42的建议):
> dput(snip)
structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0,
0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
23L, 22L, 21L, 20L, 19L, 18L, 11L, 10L), mday = c(15L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L), mon = c(11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L), year = c(115L, 115L, 115L, 115L, 115L, 115L,
115L, 115L, 115L), wday = c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), yday = c(348L, 347L, 347L, 347L, 347L, 347L, 347L, 347L, 347L
), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"), tzone = "GMT"), Reading = c(4.4,
4.62, 4.61, 6.15, 6.06, 7.04, 8.57, 4.12, 3.73)), .Names = c("date",
"Reading"), row.names = 408:416, class = "data.frame")
以下是我如何在 na.locf 文档的帮助下完成此操作的。有帮助吗?
dat<- dget("yoursample")
require(xts)
datxts<- as.xts(dat[,-1],order.by = dat$date,frequency = 24)
tzn<-tzone(datxts)
g<- seq(start(datxts), end(datxts), "hour")
gxts<- xts(rep(NA,length(g)),order.by = as.POSIXct(g), tzone = tzn)
merge(datxts,gxts,all = T)$datxts
编辑:此外,如果您将一列 NA 添加到生成的数据框中,您的方法也有效
dates=seq.POSIXt(min(snip[,1]), to=max(snip[,1]), by="1 hours")
grid = data.frame(date=dates,dummydata=rep(NA,length(dates)));
dat = merge(grid, snip, by="date", all=T)
在此先感谢您提供的任何帮助。
长话短说:我正在处理来自测量设备的每小时时间序列数据(从 SQL 导出然后导入到 R 中以便正确格式化日期时间)- 时间序列包含缺失数据,有时成组,我需要找到这些丢失的 rows/indices 并为每个包含 NA
值的实例插入一个新行。
没有解决我问题的相关问题:
Adding row to a data frame with missing values
问题数据
我在这种情况下使用的数据集相当大,并且根据我 select 的测量设备而有所不同。作为测试用例,我有一个包含 17469 小时观察值的时间序列。我找到了一小部分可用于测试目的的数据集。这是:
> snip
date Reading
408 2015-12-15 00:00:00 4.40
409 2015-12-14 23:00:00 4.62
410 2015-12-14 22:00:00 4.61
411 2015-12-14 21:00:00 6.15
412 2015-12-14 20:00:00 6.06
413 2015-12-14 19:00:00 7.04
414 2015-12-14 18:00:00 8.57
415 2015-12-14 11:00:00 4.12
416 2015-12-14 10:00:00 3.73
我们可以看到缺少 2015-12-14 12:00:00 到 2015-12-14 17:00:00 的观测值。我想先定位然后用这些日期时间填充时间序列,并在这些位置的阅读列中输入 NA
。我还想 return 附加向量中缺少的索引。
如何做到这一点?
到目前为止,我已经尝试了以下代码(如此处所建议的那样,how to add a missing dates and remove repeated dates in hourly time series),但是当我执行 merge
函数时,我最终得到的只是 NA
值,并且仍然需要确定丢失的索引所在的位置。
结果如下:
> grid = data.frame(date=seq.POSIXt(min(snip[,1]), to=max(snip[,1]), by="1 hours"));
> dat = merge(grid, snip, by="date", all.x=TRUE)
> dat
date Reading
1 2015-12-14 10:00:00 NA
2 2015-12-14 11:00:00 NA
3 2015-12-14 12:00:00 NA
4 2015-12-14 13:00:00 NA
5 2015-12-14 14:00:00 NA
6 2015-12-14 15:00:00 NA
7 2015-12-14 16:00:00 NA
8 2015-12-14 17:00:00 NA
9 2015-12-14 18:00:00 NA
10 2015-12-14 19:00:00 NA
11 2015-12-14 20:00:00 NA
12 2015-12-14 21:00:00 NA
13 2015-12-14 22:00:00 NA
14 2015-12-14 23:00:00 NA
15 2015-12-15 00:00:00 NA
我在这里错过了什么?是不是因为grid
和snip$date
顺序颠倒了?有关更多信息,请查看日期时间格式(如果这是我的问题所在):
> snip[2,1]
[1] "2015-12-14 23:00:00 GMT"
dput(snip)命令的结果如下(感谢@42的建议):
> dput(snip)
structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0,
0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
23L, 22L, 21L, 20L, 19L, 18L, 11L, 10L), mday = c(15L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L), mon = c(11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L), year = c(115L, 115L, 115L, 115L, 115L, 115L,
115L, 115L, 115L), wday = c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), yday = c(348L, 347L, 347L, 347L, 347L, 347L, 347L, 347L, 347L
), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"), tzone = "GMT"), Reading = c(4.4,
4.62, 4.61, 6.15, 6.06, 7.04, 8.57, 4.12, 3.73)), .Names = c("date",
"Reading"), row.names = 408:416, class = "data.frame")
以下是我如何在 na.locf 文档的帮助下完成此操作的。有帮助吗?
dat<- dget("yoursample")
require(xts)
datxts<- as.xts(dat[,-1],order.by = dat$date,frequency = 24)
tzn<-tzone(datxts)
g<- seq(start(datxts), end(datxts), "hour")
gxts<- xts(rep(NA,length(g)),order.by = as.POSIXct(g), tzone = tzn)
merge(datxts,gxts,all = T)$datxts
编辑:此外,如果您将一列 NA 添加到生成的数据框中,您的方法也有效
dates=seq.POSIXt(min(snip[,1]), to=max(snip[,1]), by="1 hours")
grid = data.frame(date=dates,dummydata=rep(NA,length(dates)));
dat = merge(grid, snip, by="date", all=T)