我有来自两个不同传感器的两个时间序列。由于内部时间戳设置,它们在几分钟内有所不同。我想将它们合并,忽略确切的时间:
#Timeseries 1
t = seq(as.POSIXct('2024-04-11 14:03:00'), length.out = 10, by = '10 mins')
x = c(1,3,4,2,3, 3,4, 10,6, 25)
y<- xts(x, t)
#Timeseries 2
t1 = seq(as.POSIXct('2024-04-11 14:06:00'), length.out = 10, by = '10 mins')
x1 = c(3,4, 10,6, 25, 2,3,4,2, 3)
y1 <- xts(x1, t1)
# Merge data frames
merged <- merge(y, y1, all = TRUE)
merged
像下面这样的东西应该可以工作:
library(xts)
#Timeseries 1
t = seq(as.POSIXct('2024-04-11 14:03:00'), length.out = 10, by = '10 mins')
x = c(1,3,4,2,3, 3,4, 10,6, 25)
y<- xts(x, t)
#Timeseries 2
t1 = seq(as.POSIXct('2024-04-11 14:06:00'), length.out = 10, by = '10 mins')
x1 = c(3,4, 10,6, 25, 2,3,4,2, 3)
y1 <- xts(x1, t1)
# Merge data frames
merge(y, y1, all = TRUE) |>
as.data.frame() |>
dplyr::mutate(y = ifelse(is.na(y), y1, y)) |>
subset(select = -y1) |>
xts::as.xts()
#> y
#> 2024-04-11 14:03:00 1
#> 2024-04-11 14:06:00 3
#> 2024-04-11 14:13:00 3
#> 2024-04-11 14:16:00 4
#> 2024-04-11 14:23:00 4
#> 2024-04-11 14:26:00 10
[...]
创建于 2024-11-04,使用 reprex v2.1.1