mutate on new variables inside zoo object
<p>I'm interested to compute some new variables inside a <code>zoo</code> object. I like the <code>dplyr</code>-<code>mutate</code>-way, but I can't figure out how to <em>mutate</em> inside a <code>zoo</code> object. I seem to be limited to computing on variables that already exist inside the object if I use <code>transform</code>. I can obviously do it in separate steps, but I imagine there's some handy way to work within <code>zoo</code> object I might have overlooked? I looked at <a href="https://stackoverflow.com/a/28888342/1305688">this answer</a>, which inspired my question, and <a href="https://stackoverflow.com/questions/30553761/using-dplyr-summary-function-on-yearmon-from-zoo?rq=1">this question</a>, but neither of them solved my specific issue.</p>

<p>Here is  some code demonstrating what I would like to do,</p>

<pre><code>df &lt;- data.frame(dta=paste0("2016-11-", 19:24), a = 3:8, b = 9:14)

# install.packages(c("dplyr"), dependencies = TRUE)  
library(dplyr)
df %&gt;%
   mutate(c = log(a) * log(b),
          dc = c - lag(c, 1)
          )
#          dta a  b        c        dc
# 1 2016-11-19 3  9 2.413898        NA
# 2 2016-11-20 4 10 3.192061 0.7781628
# 3 2016-11-21 5 11 3.859264 0.6672028
# 4 2016-11-22 6 12 4.452355 0.5930915
# 5 2016-11-23 7 13 4.991161 0.5388060
# 6 2016-11-24 8 14 5.487765 0.4966045

# install.packages(c("zoo"), dependencies = TRUE)
library(zoo)
df.z &lt;- read.zoo(df)

# install.packages(c("magrittr"), dependencies = TRUE)  
library(magrittr)
df.z %&gt;% transform(c = log(a) * log(b), dc = c - lag(c, 1))
# Error in x[seq_len(xlen - n)] : 
#   object of type 'builtin' is not subsettable

# the clumsy workaround
df.z.1 &lt;- df.z   %&gt;% transform(c = log(a) * log(b))
df.z.2 &lt;- df.z.1 %&gt;% transform(dc = c - lag(c, 1))
df.z.2
#            a  b        c        dc
# 2016-11-19 3  9 2.413898        NA
# 2016-11-20 4 10 3.192061 0.7781628
# 2016-11-21 5 11 3.859264 0.6672028
# 2016-11-22 6 12 4.452355 0.5930915
# 2016-11-23 7 13 4.991161 0.5388060
# 2016-11-24 8 14 5.487765 0.4966045
</code></pre>