我正在尝试获取day_high变化(突破)的一天中的时间
> df
Preco day_high day_low
2018-02-01 09:01:21 3203.5 3203.5 3202
2018-02-01 09:01:21 3203.5 3203.5 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:23 3204.5 3204.5 3202
这就是我尝试过的:
library(lubridate)
for (i in 1:150) {
if ((df$day_high[i] != df$day_high[i+1]) & (df$day_high[i] == df$day_high[i - minutes(1)])) {
print(df[[i]][i])
} else {
next
}
}
> Error in if ((df$day_high[i] != df$day_high[i + 1]) & (df$day_high[i] > == : argument is of length zero
这就是我要寻找的(day_high
变化的时期):
[1]
2018-02-01 09:01:22 3204.0 3204.0 3202
2018-02-01 09:01:23 3204.5 3204.5 3202
复制代码:
df = structure(c(3203.5, 3203.5, 3204, 3204, 3204, 3204, 3204, 3204,
3204, 3204, 3204.5, 3203.5, 3203.5, 3204, 3204, 3204, 3204, 3204,
3204, 3204, 3204, 3204.5, 3202, 3202, 3202, 3202, 3202, 3202,
3202, 3202, 3202, 3202, 3202), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", index = structure(c(1517482881,
1517482881, 1517482882, 1517482882, 1517482882, 1517482882, 1517482882,
1517482882, 1517482882, 1517482882, 1517482883), tzone = "", tclass = c("POSIXct",
"POSIXt")), .Dim = c(11L, 3L), .Dimnames = list(NULL, c("Preco",
"day_high", "day_low")))
答案 0 :(得分:3)
我们可以使用diff
并选择与其前一行不同的行。
df[diff(df$day_high) != 0, ]
# Preco day_high day_low
#2018-02-01 19:01:22 3204 3204 3202
#2018-02-01 19:01:23 3204.5 3204.5 3202