Skip to content
Prev 5792 / 15274 Next

Most common way to add derived columns to an XTS object?

The previous post was when I had something given to me already as in

library('quantmod')
getSymbols('AAPL')
CHANGE = rollapply(Cl(AAPL),width=2, function(x) log(x[2]/x[1]), by=1, align = "right", na.pad = TRUE)
merge.zoo(AAPL, CHANGE)

and this worked fine

however

I'm not using rollapply and simply want to iterate over my existing XTS object and create a new column
with a derived value.

I was trying

SD = rollapply(CHANGE, width=20, function(x) sd(x) * sqrt(252), by=1, align = "right", na.pad = TRUE)
colnames(SD) = "StdDev Log Change"
for (i in 1:NROW(SD)) {
SD[i, "STDEV"] = SD[i, "StdDev Log Change"] * Cl(AAPL)[i];
}

but it wasn't working

I'm sorry if you thought it was a duplicate question. In my mind the context where I was trying to perform
the change was different because in the original case some other function built me a series that I then
merged with my original. My question was how to I go about modifying the original without generating
a new series and merging.

In this case I'm iterating because I'm trying to perform a transformation where I map to another series
using the same index.
On Mar 10, 2010, at 8:12 AM, Joshua Ulrich wrote: