views:

35

answers:

1

How can I write this all in one line?

mydata is a zoo series, limit is a numeric vector of the same size

tmp <- ave(coredata(mydata),as.Date(index(mydata)),FUN = function(x) ( (cummax(x)-x )) )
tmp <- (tmp < limit)
final <- ave(tmp,as.Date(index(mydata)),FUN = function(x) cumprod( x) )

I've tried to use two vectors as argument to ave(...) but it seems to accept just one even if I join them into a matrix.

This is just an example, but any other function could be use.

Here I need to compare the value of cummax(mydata)-mydata with a numeric vector and once it surpasses it I'll keep zeros till the end of the day. The cummax is calculated from the beginning of each day.

If limit were a single number instead of a vector (with different possible numbers) I could write it:

ave(coredata(mydata),as.Date(index(mydata)),FUN = function(x) cumprod( (cummax(x)-x ) < limit) )

But I can't introduce there a vector longer than x (it should have the same length than each day) and I don't know how to introduce it as another argument in ave()

cheers