# [R-SIG-Finance] efficient extraction of local extrema and zero-crossings in large multivariate zoo?

Christian Gunning icos.atropa at gmail.com
Tue Jun 30 12:41:36 CEST 2009

```Thanks so much for all for the input - approaching this from several
directions helped me understand the problem in greater depth.

As an aside, I see now that which.min() picks the first min, so i've
used "x[x<0] <- NA" instead (see end).

Using apply.daily, the function scales linearly all the way up.  With
the improved zoo function, i see significant super-linear scaling of
time, with the apply.daily version winning on this machine above ~2e4
total samples (and losing below). I didn't look at scaling of
memory...

thanks again,
christian gunning

#### locate max and zero-crossings, interpolate between
find.extrema = function(myzoo) {
days = as.POSIXct(levels(as.factor(as.Date(index(myzoo)))))
ret = myzoo
ret[] <- NA
for (tcol in 1:dim(myzoo)[2]) {
this = myzoo[,tcol]
max.days <- apply.daily(this,function(x) { index(x)[which.max(x)] } )
max.days <- this[max.days]
this[this<0] <- NA
min.days <- apply.daily(this,function(x) { index(x)[which.min(x)] } )
min.days <- this[min.days]
ret[,tcol] <- cbind(ret[,tcol],rbind(min.days,max.days))[,2]
} # end tcol
ret = na.approx(ret, na.rm=F)
return(ret)
}

```