[R] Simplify, several Ave or aggregates

skan juanpide at gmail.com
Wed Sep 29 18:42:26 CEST 2010


Hello.

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(coredata(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 maxtrix.

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 (with the length of each
day) and I don't know how to introduce it as another argument in ave()

cheers
-- 
View this message in context: http://r.789695.n4.nabble.com/Simplify-several-Ave-or-aggregates-tp2719363p2719363.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list