连续值高于阈值的位置和值

我需要找到我的数据在连续几天达到阈值的位置。 我正在寻找高于阈值的4个连续观察值。 我想返回符合这些标准的系列的第一个观察点的位置。

以下是一个示例数据集:

eg = structure(list(t.date = structure(c(1L, 2L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("4/30/11", 
"5/1/11", "5/10/11", "5/11/11", "5/12/11", "5/13/11", "5/14/11", 
"5/15/11", "5/16/11", "5/17/11", "5/2/11", "5/3/11", "5/4/11", 
"5/5/11", "5/6/11", "5/7/11", "5/8/11", "5/9/11"), class = "factor"), 
t.avg = c(4L, 4L, 5L, 6L, 10L, 18L, 18L, 18L, 18L, 12L, 10L, 
10L, 8L, 8L, 9L, 10L, 6L, 5L)), .Names = c("date", "avg"
), row.names = c(NA, -18L), class = "data.frame")

我想要平均值符合标准的日期(4天内平均> 17)一种方法:

eg$date %in% eg$date[which(eg$avg > 17)]
# [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
# [13] FALSE FALSE FALSE FALSE FALSE FALSE

在这种情况下,我可以将第一个TRUE作为答案,但如果第二个,第三个或第四个不是TRUE

我需要条件为TRUE的第一个日期:

eg$date[which(eg$avg > 17)]
# [1] 5/5/11 5/6/11 5/7/11 5/8/11

以及系列中第一次观察的位置:

which(eg$avg > 17)
# [1] 6 7 8 9

我发现了相关的问题,但是我一直无法根据需要来改变方法。

非常感谢。


library(zoo)
#  Get the index value
xx <- which(rollapply(eg$avg,4, function(x) min(x))>17)[1]
# Get the date
eg$date[xx]

使用运行长度编码( rle

> rle(eg$avg > 17)
Run Length Encoding
  lengths: int [1:3] 5 4 9
  values : logi [1:3] FALSE TRUE FALSE

rleg <- rle(eg$avg > 17)
rleg$lengths[!rleg$values][1]  # returns so add one to it 
#Only works in this case b/c no test for length of run Gt 17
# if first 4 all gt 17 then return 1
# else return 1+ cumsum of lengths up to first true with length Gt or equal to 4

# The code to do that.

 if (rleg$values[1] && rleg$lengths[1] >=4 ) {1} else{
     1+ cumsum( rleg$lengths[1:which(rleg$lengths >=4 & 
                                     rleg$values)][1])}
#[1] 6

可以使用base R来完成:

eg$th = ifelse(eg$avg>17, 1,0)
for(i in 4:nrow(eg)) {if(sum(eg$th[(i-3):i])>3) print(i-3)}
[1] 6

实际日期:

for(i in 4:nrow(eg)) {if(sum(eg$th[(i-3):i])>3) print(eg[i-3,1])}
[1] 5/5/11
链接地址: http://www.djcxy.com/p/23567.html

上一篇: Location and value for consecutive values above threshold

下一篇: Selenium WebDriver tests with JavaScript disabled