[英]Insert missing rows in time series data
I have an incomplete time series dataframe and I need to insert rows of NAs for missing time stamps.我有一个不完整的时间序列数据框,我需要为缺少的时间戳插入 NA 行。 There should always be 6 time stamps per day, which is indicated by the variable "Signal" (1-6) in the dataframe.
每天应该总是有 6 个时间戳,这由数据帧中的变量“信号”(1-6)指示。 I am trying to merge the incomplete dataframe
A
with a vector B
containing all Signals.我正在尝试将不完整的数据帧
A
与包含所有信号的向量B
合并。 Simplified example data below:下面的简化示例数据:
B <- rep(1:6,2)
A <- data.frame(Signal = c(1,2,3,5,1,2,4,5,6), var1 = c(1,1,1,1,1,1,1,1,1))
Expected <- data.frame(Signal = c(1,2,3,NA, 5, NA, 1,2,NA,4,5,6), var1 = c(1,1,1,NA,1,NA,1,1,NA,1,1,1)
Note that B
represents a dataframe with multiple variables and the NAs in Expected
are rows of NAs in the dataframe.请注意,
B
表示具有多个变量的数据帧, Expected
中的 NA 是数据帧中的 NA 行。 Also the actual dataframe has more observations (84 in total).此外,实际的数据框有更多的观察结果(总共 84 个)。
Would be awesome if you guys could help me out!如果你们能帮助我,那就太棒了!
If you already know there are 6 timestamps in a day you can do this without B
.如果您已经知道一天有 6 个时间戳,则可以在没有
B
情况下执行此操作。 We can create groups for each day and use complete
to add the missing observations with NA
.我们可以为每一天创建组,并使用
complete
用NA
添加缺失的观察结果。
library(dplyr)
library(tidyr)
A %>%
group_by(gr = cumsum(c(TRUE, diff(Signal) < 0))) %>%
complete(Signal = 1:6) %>%
ungroup() %>%
select(-gr)
# Signal var1
# <dbl> <dbl>
# 1 1 1
# 2 2 1
# 3 3 1
# 4 4 NA
# 5 5 1
# 6 6 NA
# 7 1 1
# 8 2 1
# 9 3 NA
#10 4 1
#11 5 1
#12 6 1
If in the output you need Signal
as NA
for missing combination you can use如果在输出中您需要将
Signal
作为NA
用于缺少组合,您可以使用
A %>%
group_by(gr = cumsum(c(TRUE, diff(Signal) < 0))) %>%
complete(Signal = 1:6) %>%
mutate(Signal = replace(Signal, is.na(var1), NA)) %>%
ungroup %>%
select(-gr)
# Signal var1
# <dbl> <dbl>
# 1 1 1
# 2 2 1
# 3 3 1
# 4 NA NA
# 5 5 1
# 6 NA NA
# 7 1 1
# 8 2 1
# 9 NA NA
#10 4 1
#11 5 1
#12 6 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.