title: using apply in R with a user defined function
body: <p>I am trying to apply the user defined function augenSpike over a set of tickers stored in an environment but somehow it is not working. If someone can help this entry level R user, I would much appreciated...</p>
  
  <pre><code>library(quantmod)
  
  slideapply &lt;- function(x, n, FUN=sd) {
    v &lt;- c(rep(NA, length(x)))
    for (i in n:length(x) ) {
      v[i] &lt;- FUN(x[(i-n+1):i])
    } 
    return(v)   
  }
  
  augenSpike &lt;- function(x, n=20) {
    prchg &lt;- c(NA, diff(x))
    lgchg &lt;- c(NA, diff(log(x)))
    stdevlgchg &lt;- slideapply(lgchg, n, sd)
    stdpr &lt;- x * stdevlgchg
    #shuffle things up one
    stdpr &lt;- c(NA, stdpr[-length(stdpr)])
    spike &lt;- prchg / stdpr
    return(spike)
  }
  myenv &lt;- new.env()
  # environment used to store tickers
  tickers &lt;- c("PBR", "AAPL", "MSFT", "GOOG")
  getSymbols(tickers, env= myenv)
  sp &lt;-tickers['2013/2014']
  asp &lt;- augenSpike(as.vector(Cl(sp)))
  sp$spike &lt;- asp
  
  
  ## Create a vector of colors selected based on whether x is &lt;0 or &gt;0
  ## (FALSE + 1 -&gt; 1 -&gt; "blue";    TRUE + 1 -&gt; 2 -&gt; "red")
  cols &lt;- c("blue", "red" ) [(sp$spike &gt; 0) +  1]
  
  barplot(sp['2013-2014']$spike, col= cols, main="Augen Price Spike", xlab="Time Daily",ylab="Price Spike in Std Dev")
  abline(h = 2, col = "red")
  abline(h = 0, col = "black")
  abline(h = -2, col = "red")
  </code></pre>