删除 xts 中重复的邻居值
Remove neighbour values that are duplicates in xts
在 [xts1$master_decision] 中,我试图删除值与上面一个单元格的值相同的行。我的目标是在不涉及任何其他包的情况下使用 R base 执行此操作。
如果有办法解决这个向量化问题,跳过 for 循环,那也很好。
# --------------------------------------
# Construct xts data.
# --------------------------------------
rows_to_build <- 6
dates <- seq(
as.POSIXct("2019-01-01 09:01:00"),
length.out = rows_to_build,
by = "1 min",
tz = "CEST"
)
master_decision = c(
# - Clarification what "for-loop" should do:
3, # Keep (missing [3] in cell above)
2, # Keep (missing [2] in cell above)
2, # Delete due to [2] in cell above)
3, # Keep (missing [3] in cell above)
3, # Delete due to [3] in cell above)
2 # Keep (missing [2] in cell above)
)
data <- data.frame(master_decision)
xts1 <- xts(x = data, order.by = dates)
rm(list = ls()[! ls() %in% c("xts1")]) # Only keep [xts1].
# ------------------------------------------------------------
# For loop with purpose to remove duplicates that are grouped.
# ------------------------------------------------------------
for (i in 2:nrow(xts1)) {
if(xts1[[i]] == xts1[[i-1]]) {
xts1[-c(i)]
}
}
xts1 在 运行 for 循环之前:
master_decision
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:03:00 2
2019-01-01 09:04:00 3
2019-01-01 09:05:00 3
2019-01-01 09:06:00 2
结果(时间戳为 [09:04:00] 的行已删除:
master_decision
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:03:00 2
2019-01-01 09:04:00 3
2019-01-01 09:06:00 2
想要的结果:(带有时间戳 [09:04:00] 和 [09:05:00] 的行已删除
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:04:00 3
2019-01-01 09:06:00 2
您可以使用 zoo
中的 coredata
并通过对数据进行子集化来保留与先前值不同的值。
library(zoo)
xts1[c(TRUE, coredata(xts1)[-length(xts1)] != coredata(xts1)[-1]), ]
# master_decision
#2019-01-01 09:01:00 3
#2019-01-01 09:02:00 2
#2019-01-01 09:04:00 3
#2019-01-01 09:06:00 2
或者要将其完全保留在基数 R 中,请使用 as.numeric
xts1[c(TRUE, as.numeric(xts1)[-length(xts1)] != as.numeric(xts1)[-1]), ]
另一种选择是使用 head
/tail
而不是 -length(xts1)
和 -1
来子集
xts1[c(TRUE, tail(as.numeric(xts1), -1) != head(as.numeric(xts1), -1)), ]
这也能起到作用。获取相同值序列的第一个索引,然后按这些索引进行过滤。
idx <-cumsum(c(1,rle(master_decision)$lengths))
idx <- idx[-length(idx)]
xts1 <- xts(x = master_decision[idx], order.by = dates[idx])
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:04:00 3
2019-01-01 09:06:00 2
在 [xts1$master_decision] 中,我试图删除值与上面一个单元格的值相同的行。我的目标是在不涉及任何其他包的情况下使用 R base 执行此操作。
如果有办法解决这个向量化问题,跳过 for 循环,那也很好。
# --------------------------------------
# Construct xts data.
# --------------------------------------
rows_to_build <- 6
dates <- seq(
as.POSIXct("2019-01-01 09:01:00"),
length.out = rows_to_build,
by = "1 min",
tz = "CEST"
)
master_decision = c(
# - Clarification what "for-loop" should do:
3, # Keep (missing [3] in cell above)
2, # Keep (missing [2] in cell above)
2, # Delete due to [2] in cell above)
3, # Keep (missing [3] in cell above)
3, # Delete due to [3] in cell above)
2 # Keep (missing [2] in cell above)
)
data <- data.frame(master_decision)
xts1 <- xts(x = data, order.by = dates)
rm(list = ls()[! ls() %in% c("xts1")]) # Only keep [xts1].
# ------------------------------------------------------------
# For loop with purpose to remove duplicates that are grouped.
# ------------------------------------------------------------
for (i in 2:nrow(xts1)) {
if(xts1[[i]] == xts1[[i-1]]) {
xts1[-c(i)]
}
}
xts1 在 运行 for 循环之前:
master_decision
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:03:00 2
2019-01-01 09:04:00 3
2019-01-01 09:05:00 3
2019-01-01 09:06:00 2
结果(时间戳为 [09:04:00] 的行已删除:
master_decision
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:03:00 2
2019-01-01 09:04:00 3
2019-01-01 09:06:00 2
想要的结果:(带有时间戳 [09:04:00] 和 [09:05:00] 的行已删除
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:04:00 3
2019-01-01 09:06:00 2
您可以使用 zoo
中的 coredata
并通过对数据进行子集化来保留与先前值不同的值。
library(zoo)
xts1[c(TRUE, coredata(xts1)[-length(xts1)] != coredata(xts1)[-1]), ]
# master_decision
#2019-01-01 09:01:00 3
#2019-01-01 09:02:00 2
#2019-01-01 09:04:00 3
#2019-01-01 09:06:00 2
或者要将其完全保留在基数 R 中,请使用 as.numeric
xts1[c(TRUE, as.numeric(xts1)[-length(xts1)] != as.numeric(xts1)[-1]), ]
另一种选择是使用 head
/tail
而不是 -length(xts1)
和 -1
来子集
xts1[c(TRUE, tail(as.numeric(xts1), -1) != head(as.numeric(xts1), -1)), ]
这也能起到作用。获取相同值序列的第一个索引,然后按这些索引进行过滤。
idx <-cumsum(c(1,rle(master_decision)$lengths))
idx <- idx[-length(idx)]
xts1 <- xts(x = master_decision[idx], order.by = dates[idx])
2019-01-01 09:01:00 3
2019-01-01 09:02:00 2
2019-01-01 09:04:00 3
2019-01-01 09:06:00 2