Skip to content

error msg using na.approx "x and index must have the same length"

2 messages · Jay, R. Michael Weylandt

2 days later
#
On Fri, Oct 12, 2012 at 1:26 AM, Jay Rice <jsrice18 at gmail.com> wrote:
Your problem is that this splits portfolio$Price by St but not
timestamp, so the number of timestamps passed to na.approx() doesn't
align with the Price series. I think you want something more like
this:

lapply(split(portfolio[,-1], portfolio$St), function(x)
zoo(na.approx(x[,2], x[,1]), x[,1]))

which is admittedly opaque.

I think an easier data management strategy for you might be to put
your data in a list of zoo/xts series and use lapply generously.

E.g.,

pp <- lapply(split(portfolio[,-1], portfolio$St), as.zoo)

and then do your calculations with generous use of lapply()

Cheers,
Michael