r - 连续超过阈值和 R 中的附加条件
问题描述
我想使用 R 获得满足以下条件的时间序列中的时间步长(应该是满足以下条件的第一个时间步长):
[1] V1 > 0 at the time step
[2] V1 > 0 in at least 3 consecutive time steps from the timestep obtained in [1]
[3] Accumulated value of the next four timesteps following [1] should be greater than 1.
这是数据
structure(list(V1 = c(-3.85326, -2.88262, -4.1405, -3.95193,
-6.68925, -2.04202, -2.47597, -4.91161, -2.5946, -2.82873, 2.68839,
-4.1287, -4.50296, -0.143476, -1.12174, -0.756168, -1.67556,
-1.92704, -1.89279, -2.37569, -5.71746, -2.7247, -4.12986, -2.29769,
-1.52835, -2.63623, -2.31461, 2.32796, 4.14354, 4.47055, -0.557311,
-0.425266, -2.37455, -5.97684, -5.22391, 0.374004, -0.986549,
2.36419, 0.218283, 2.66014, -3.44225, 3.46593, 1.3309, 0.679601,
5.42195, 10.6555, 8.34144, 1.64939, -1.64558, -0.754001, -4.77503,
-6.66197, -4.07188, -1.72996, -1.15338, -8.05588, -6.58208, 1.32375,
-3.69241, -5.23582, -4.33509, -7.43028, -3.57103, -10.4991, -8.68752,
-8.98304, -8.96825, -7.99087, -8.25109, -6.48483, -6.09004, -7.05249,
-4.78267)), class = "data.frame", row.names = c(NA, -73L))
到目前为止我所拥有的
我能够结合条件 1 和 2。这是脚本。
first_exceed_seq <- function(x, thresh = 0, len = 3)
{
# Logical vector, does x exceed the threshold
exceed_thresh <- x > thresh
# Indices of transition points; where exceed_thresh[i - 1] !=
exceed_thresh[i]
transition <- which(diff(c(0, exceed_thresh)) != 0)
# Reference index, grouping observations after each transition
index <- vector("numeric", length(x))
index[transition] <- 1
index <- cumsum(index)
# Break x into groups following the transitions
exceed_list <- split(exceed_thresh, index)
# Get the number of values exceeded in each index period
num_exceed <- vapply(exceed_list, sum, numeric(1))
# Get the starting index of the first sequence where more then len
exceed thresh
transition[as.numeric(names(which(num_exceed >= len))[1])]
}
然后,使用上面的函数,只需键入:
first_exceed_seq(dat[,1])
这给出了 28。这应该是正确的答案,但我想知道以下问题。
问题
1)我想在上面的函数中添加第三个条件,使得 29 到 32 的总和大于 1。从上面的函数中,我将最小长度设置为 3。我将把它应用于多个时间序列,我可能会遇到具有四个连续正值或更多的时间序列,并且由此开始的第一个时间步不满足 [3],而是第二个或第三个时间步等。
有关如何执行此 R 的任何建议?我会很感激任何帮助。
更新:我尝试了下面的解决方案,但 dplyr 给出了警告消息。
1:在 filter_impl(.data, quo) 中:强制混合评估
lead
。请使用 dplyr::lead() 或 library(dplyr) 删除此警告。
正确答案也应该是 28,因为它首先满足所有三个条件。
解决方案
这是使用dplyr
包和lead
功能的解决方案。在以下代码中,x
是您提供的数据:
library(dplyr)
newx <- x %>% as_tibble() %>%
mutate(time = 1: n()) %>%
filter(V1 > 0, lead(V1, 1) > 0, lead(V1, 2) > 0,
lead(V1, 1) + lead(V1, 2) + lead(V1, 3) + lead(V1, 4) > 1)
# A tibble: 7 x 2
V1 idx
<dbl> <int>
1 2.33 28
2 2.36 38
3 3.47 42
4 1.33 43
5 0.680 44
6 5.42 45
7 10.7 46
如果您只想要第一次出现,您可以使用slice
:
slice(newx, 1)
# A tibble: 1 x 2
V1 idx
<dbl> <int>
1 2.33 28
关于错误:要么dplyr
像我一样包含包,要么替换lead
为filter::lead
.
推荐阅读
- spring-boot - 使用 Spring-Boot 在 http 中返回 jax-rs 响应时出错
- angular - Tap 算子在 RxJs 中带来了什么有用的功能?
- firebase - 如何开玩笑测试是否在 RN 应用程序的 ComponentDidMount 中调用 Firebase Analytics
- react-native - 不变违规 webview 已从 react native 中删除
- sql - 将相同的 ID 添加到相同的行
- algorithm - 打印最长回文子串 (LPS)
- r - 如何将导入的向量分隔为 R 中的列?
- javascript - 在具有任意数量子节点的树中,查找节点是在给定节点的左半子树还是右半子树中
- python - 如何指定在 PuLP python 中使用的方法?
- amazon-web-services - 用于模型托管的 AWS Sagemaker 与 ECS