0

This is head of data G

     Date      Open     Close   Volume   Adj.Close
   2016-03-03  718.68   712.42  1958000    712.42
   2016-03-04  714.99   710.89  1972100    710.89
   2016-03-07  706.90   695.16  2985100    695.16
   2016-03-08  688.59   693.97  2076300    693.97
   2016-03-09  698.47   705.24  1421500    705.24
   2016-03-10  708.12   712.82  2833500    712.82

and this is my function:

r <- c(1:(nrow(G)-1))
w <-  c(1:(nrow(G)-2))

R <- function(j){

  for(j in 1:((nrow(G)-1)))        #calculating daily returns

  { 
    r[j] <- (G[j+1,7]/G[j,7])
    }


   for(i in 10:((nrow(G)-1)))
   {
     w[i] <- (mean(r[(i-9):i]))        #calculating moving average

      }
   return(w)
     }
q <- vectorize(R)
curve(q)

This is the error I get
Error in xy.coords(x, y, xlabel, ylabel, log) : (list) object cannot be coerced to type 'double'

I checked various posts, but nothing is helping. Thanks in advance.

r_hudson
  • 193
  • 8
  • I'm not exactly sure what you want your function to return. Is it the vector `r` that you want to plot? Also,`q` is a function, so `curve(q)` tries to run `curve` on `q` (the function itself), rather than on the *output* of `q`, which is what I assume you actually meant to do. Also, `curve` operates on expressions, rather than on vectors of numbers, so you probably need `plot` rather than `curve`. – eipi10 Mar 04 '17 at 22:21
  • For daily returns, you can do `G$Close/lag(G$Close)`. For the moving average, see `rollmean` from the `zoo` package. – eipi10 Mar 04 '17 at 22:26
  • Thank you so much for the reply. I want to plot moving averages. So I am combining daily returns and moving averages in one function and I want to plot the moving averages. I will try to do it with zoo package. – r_hudson Mar 04 '17 at 22:38
  • Code in a comment isn't very helpful. Please put formatted code into the body of your question. – eipi10 Mar 04 '17 at 22:40
  • I modified the code, and I want to plot w – r_hudson Mar 04 '17 at 22:44
  • Got it. f <- R() and then plotted f. Thanks for probing what I wanted to plot, which led me to this. – r_hudson Mar 04 '17 at 23:13

0 Answers0