[英]Finding the maximum in an xts column within the last X hrs
I have a dataset within a reactive dygraph that looks like this:我在反应式 dygraph 中有一个数据集,如下所示:
data()$data.o数据()$data.o
date![]() |
data.o![]() |
---|---|
2022-07-21 12:10 AM ![]() |
400.1 ![]() |
2022-07-21 12:11 AM ![]() |
33.9 ![]() |
2022-07-21 12:12 AM ![]() |
32.5 ![]() |
2022-07-21 12:13 AM ![]() |
35.1 ![]() |
2022-07-21 12:14 AM ![]() |
31.5 ![]() |
2022-07-21 12:15 AM ![]() |
39.5 ![]() |
I want to find the max value in the last 5 minutes so I can set my axis scale accordingly.我想在最后 5 分钟内找到最大值,以便相应地设置轴刻度。
I've tried:我试过了:
enddate = max(data()$date)
startdate = enddate - (60*5)
oMx <- max(datao.xts[startdate/enddate], na.rm = T)
But I get an error using datao.xts.但是使用 datao.xts 时出现错误。
Is there a better way to go about this?有没有更好的方法来解决这个问题?
As far as I'm familiar with xts
, your startdate/enddate
part has simply to be a string, cf Joshua's response here .据我熟悉
xts
,您的startdate/enddate
部分必须是一个字符串,参见 Joshua's response here 。 That's it.而已。
# allow me to create an xts object beforehand
datetimes <- c("2022-07-21 12:10 AM",
"2022-07-21 12:11 AM",
"2022-07-21 12:12 AM",
"2022-07-21 12:13 AM",
"2022-07-21 12:14 AM",
"2022-07-21 12:15 AM") |>
strptime(format = "%Y-%m-%d %I:%M %p") |>
as.POSIXct()
data <- c(400.1, 33.9, 32.5, 35.1, 31.5, 39.5)
xts <- xts::xts(data, order.by = datetimes)
# minor adjustments to your approach
enddate <- zoo::index(xts) |> max()
startdate <- enddate - (5-1) * 60
xts[paste0(startdate, enddate, sep = "/")] |> max()
#> [1] 39.5
# assuming you are interested in the last five observations
xts |> utils::tail(5) |> max()
#> [1] 39.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.