title: applying seasons including yearly and half-year in R
body: <p>I know there are answers on here on how to apply seasons to a data frame, my question adds the complexity of adding yearly and half-year when a start to end date doesn't fall within the typical season range</p>
  
  <pre><code>dates &lt;- data.frame(StartDate=as.Date(c("01/01/2013","04/01/2013","10/01/2013","06/01/2013"    
  ,"09/01/2013","05/01/2013"), format = "%m/%d/%Y"),EndDate=as.Date(c("12/01/2013","12/21/2013
  ","05/25/2014","08/15/2013","11/30/2013","10/01/2013"),format = "%m/%d/%Y"))
  
  
  StartDate    EndDate
  1 2013-01-01 2013-12-01
  2 2013-04-01 2013-12-21
  3 2013-10-01 2014-05-25
  4 2013-06-01 2013-08-15
  5 2013-09-01 2013-11-30
  6 2013-05-01 2013-10-01
  </code></pre>
  
  <p>I need to write a function that adds a "season" column so my output so I would add a column named season to the existing data frame </p>
  
  <pre><code>    StartDate    EndDate  Season
  1 2013-01-01 2013-12-01   Yearly
  2 2013-04-01 2013-12-21   Yearly
  3 2013-10-01 2014-05-25   Half-year
  4 2013-06-01 2013-08-15   Summer
  5 2013-09-01 2013-11-30   Fall
  6 2013-05-01 2013-10-01   Half-year
  </code></pre>
  
  <p>Also, since the dates fall are all over the place I thought it might simplify things if I drop the date and year and applying the function based on the month only and then I converted them to numeric. </p>
  
  <pre><code>dates$StartDate &lt;- format(dates$StartDate, "%m")
  dates$EndDate &lt;- format(dates$EndDate, "%m")
  dates$StartDate &lt;- as.numeric(dates$StartDate)
  dates$StartDate &lt;- as.numeric(dates$StartDate)
  
   StartDate    EndDate
    1            12
    4            12
   10             5
    6             8
    9            11
    5            10
  </code></pre>
  
  <p>Here is the function that I tried to write. My parameters are: if start date equals end date then that's yearly, if end date - start date +1 = 12 then that's yearly, if end date - start date +1 is between 8-11 that's yearly, if end date - start date +1 is greater than or equal to 5 but less than 8 then that's half-year, if end date - start date = 6 that is half-year, and then the season based on 3 month intervals. </p>
  
  <p>If there is an easier way I am open to suggestions.</p>
  
  <pre><code>Seasons &lt;- function(dates) 
  {
  dates$Season &lt;- NULL
  for(i in 1:dim(dates)[1])
  {
      if(dates$StartDate[i] == dates$EndDate[i]){
          dates$Season[i] &lt;- "Yearly"  
      }
      if(dates$EndDate[i] - dates$StartDate[i] + 1 == 12){
          dates$Season[i] &lt;- "Yearly" 
      }
      if(dates$EndDate[i] - dates$StartDate[i] == 6){
           dates$Season[i] &lt;- "Half Year"
      }
      if(dates$EndDate[i] - dates$StartDate[i] + 1 &gt;= 5 &lt; 8){
          dates$Season[i] &lt;- "Half Year" 
      }
      if(dates$EndDate[i] - dates$StartDate[i] + 1 &gt;= 8 &lt; 12){
          dates$Season[i] &lt;- "Yearly"
      }
      if(dates$StartDate[i] == 12 &amp; dates$EndDate[i] == 2){
          dates$Season[i] &lt;- "Winter"
      }
      if(dates$StartDate[i] == 3 &amp; dates$EndDate[i] == 5){
          dates$Season[i] &lt;- "Spring"
      }
      if(dates$StartDate[i] == 6 &amp; dates$EndDate[i] == 8){
          dates$Season[i] &lt;- "Summer"
      }
      if(dates$StartDate[i] == 9 &amp; dates$EndDate[i] == 11){
          dates$Season[i] &lt;- "Fall"
      }
  return(dates)
  }
  }
  </code></pre>
  
  <p>When I run the function it applies "Summer" to all dates. Also, there are rows that are blank or have no end date which I want to ignore.</p>
  
  <p>I also get the a lot of errors, here are the main ones: </p>
  
  <pre><code>Error: unexpected '&lt;' in:
  Error in dates$Season[i] &lt;- "Half Year" : object 'i' not found
  Error: unexpected '}' in "        }"
  </code></pre>