2014-09-23 31 views
1

我有两列的时间序列,一列包含一个“信号”,或者是NA或不是(然后它是1到5之间的整数,不关心实际值,除非它与NA不同),第二列包含实际值。R找到时间序列的动态窗口的最大值(或最小值)

我需要计算自信号不是NA以来的最高值。这在下面的例子中显示。

Date  Sig Val  Expected result 
    2008-01-01 1 47  47 <<- Sig==1, i. e. here we start counting 
    2008-01-02 NA 31  47 
    2008-01-03 NA 61  61 <<- 61 is higher than 47, so this one is important now 
    2008-01-04 NA 43  61 
    2008-01-05 NA 23  61 
    2008-01-06 NA 46  61 
    2008-01-07 NA 17  61 
    2008-01-08 NA 52  61 
    2008-01-09 NA 84  84 <<- a new high, value should be kept 
    2008-01-10 NA 54  84 
    2008-01-11 1 30  30 <<- a new signal, here we start counting again 
    2008-01-12 NA 36  36 <<- a new higher value in this segment 
    2008-01-13 NA 59  59 <<- again a new higher value in this segment 
    2008-01-14 NA 56  59 
    2008-01-15 NA 15  59 
    2008-01-16 NA 21  59 
    2008-01-17 NA 87  87 
    2008-01-18 NA 81  87 
    2008-01-19 2 94  94 <<- a new signal this time a 2, so here we start counting again 
    2008-01-20 NA 42  94 
    2008-01-21 NA 95  95 
    2008-01-22 1 42  42 <<- a new signal, here we start counting again 
    2008-01-23 NA 25  42 
    2008-01-24 NA 20  42 
    2008-01-25 NA 76  76 
    2008-01-26 NA 95  95 
    2008-01-27 NA 14  95 
    2008-01-28 NA 12  95 
    2008-01-29 NA 13  95 
    2008-01-30 NA 57  95 
    2008-01-31 NA 26  95 

问题是相关观测窗口是动态的,取决于信号列。

我玩过runMax(),cummax()和rollapply()函数,但它们只能在特定的窗口长度上工作......我想我看不到树木,但我无法弄清楚如何使“回顾”窗口动态化。任何猜测?

回答

4

创建一个用于创建信号组一列,然后使用cummax

使用data.table(假设你的数据在一个data.frame DF)

library(data.table) 

D <- as.data.table(df) 
D[, list(maxvalue = cummax(value)), 
    by = list(sig2 = cumsum(replace(sig, is.na(sig), 0)))] 
+0

您好,感谢。大!首先它没有按预期工作,因为我不知道我的“Sig”也包含一些负值。但是,在replace()周围添加一个abs()解决了这个问题。 – Alen 2014-09-23 16:58:53