title: Error in 2:n : NA/NaN argument
body: <p>The code below produces the following error:</p>
  
  <blockquote>
    <p><code>Error in 2:n : NA/NaN argument</code> </p>
  </blockquote>
  
  <p>How can I resolve this error?</p>
  
  <pre><code>library (pdfetch)
  library(tidyverse)
  library(xts)
  tickers&lt;-c("AXP","MMM","BA","CAT","CVX","CSCO","KO","DWDP","AAPL","XOM","GE","GS","HD","IBM","INTC","HPI","AIV","MCD","MRK","MSFT","NKE","PFE","PG","TRV","JPM","UTX","VZ","V","WMT","DIS")
  
  data&lt;-pdfetch_YAHOO(tickers&lt;- c("^DJI","AXP","MMM","BA","CAT","CVX","CSCO","KO","DWDP","AAPL","XOM","GE","GS","HD","IBM","INTC","HPI","AIV","MCD","MRK","MSFT","NKE","PFE","PG","TRV","JPM","UTX","VZ","V","WMT","DIS"),from = as.Date("2015-03-20"),to =     as.Date("2018-03-20"),interval='1mo')
  
  # to remove the nas from the entire data
  data[complete.cases(data),]
  plus&lt;-data[complete.cases(data),]
  plus
  
  str(plus)
  head(plus)
  tail(plus)
  class(plus$Date)
  (plus[1:10, "^DJI.adjclose",drop=F])
  
  #Create a new data frame that contains the price data with the dates as the row names
  prices &lt;- (plus)[, "^DJI.adjclose", drop = FALSE]
  rownames(prices) &lt;-plus$Date
  head(prices)
  tail(prices)
  
  #to find the return from 3/3/2015-3/8/2018
  djia_ret1&lt;- ((prices [2:n,1]-prices [1:(n-1),1])/prices [1:(n-1),1])
  </code></pre>