For loop not iterating through all values in vector
<p>I am trying to sum the NA values per year of multiple time series. I have created a vector of just my dates that I am then comparing to a vector of both dates and ozone values. I have first created a for loop that iterates through all the dates of my dates vector to extract just the years. I am then comparing the years vector to the years in the vector of both ozone and date values (this is where my issue is) I then hope to put each year into a vector and sum the NA values. When I attempt to iterate through the years in the date ozone vector I have either created an infinite loop or only pulled the first variable. </p>

<p>I have tried several different methods to iterate the vector of both dates and times but am very stuck. The other things I have tried are listed below as well as my code:</p>

<pre><code>totalNA &lt;- c()
for (i in npsfiles[1]) {
  timeozone &lt;- import(i, date="DATE", date.format = "%m/%d/%Y", header=TRUE, na.strings="-999")
  O3 &lt;- c(timeozone$O3)
  tyr &lt;- substr(timeozone$date, 1, 10)
  ozone &lt;- O3
  names(ozone) &lt;- tyr
  for (d in as.character(tyr)) {
    theyears &lt;- format.Date(d, "%Y")
    print(theyears)
    for (y in length(ozone)) {
      if (names(ozone[y]) == theyears) {
      totalNA &lt;- c(totalNA, ozone[y])}
      print(y)
      }
  }   
} 
</code></pre>

<p>My issue is at the third for loop. I have also tried: </p>

<pre><code>for (y in seq(1:length(ozone))
for (y in ozone)
for (y in names(ozone))  
</code></pre>

<p>Here is some data:</p>

<pre><code>ABBR            DATE         O3
BADL-VC 10/01/1987 02   -999
BADL-VC 10/01/1987 03   -999
BADL-VC 01/01/1989 10   12
BADL-VC 01/01/1989 11   13
BADL-VC 01/01/1989 12   14
</code></pre>