title: Reshaping EPA wind speed & direction data with dcast in R
body: <p>I am trying to convert long format wind data into wide format. Both wind speed and wind direction are listed within the Parameter.Name column. These values need to be cast by both Local.Site.Name, and Date.Local variables. </p>
  
  <p>If there are multiple observations per unique Local.Site.Name + Date.Local row, then I want the mean value of those observations. The built-in argument 'fun.aggregate = mean' works just fine for wind speed, but mean wind direction cannot be computed this way because the values are in degrees. For example, the average of two wind directions near North (350, 10) would output as South (180). For example: ((350 + 10)/2 = 180), despite the polar average being 360 or 0. </p>
  
  <p>The 'circular' package will allow us to compute the mean wind direction without having to perform any trigonometry, but I am having trouble trying to nest this additional function within the 'fun.aggregate' argument. I thought a simple else if statement would do the trick, but I am running into the following error:</p>
  
  <pre><code>Error in vaggregate(.value = value, .group = overall, .fun = fun.aggregate, : could not find function ".fun"
  In addition: Warning messages:
  1: In if (wind$Parameter.Name == "Wind Direction - Resultant") { :
      the condition has length &gt; 1 and only the first element will be used
  2: In if (wind$Parameter.Name == "Wind Speed - Resultant") { :
      the condition has length &gt; 1 and only the first element will be used     
  3: In mean.default(wind$"Wind Speed - Resultant") :
      argument is not numeric or logical: returning NA
  </code></pre>
  
  <p>The goal is to be able to use the <code>fun.aggregate = mean</code> for Wind Speed, but the <code>mean(circular(Wind Direction, units = 'degrees')</code> for Wind Direction. </p>
  
  <p>Here's the original data (>100MB):
      <a href="https://drive.google.com/open?id=0By6o_bZ8CGwuUUhGdk9ONTgtT0E" rel="nofollow noreferrer">https://drive.google.com/open?id=0By6o_bZ8CGwuUUhGdk9ONTgtT0E</a></p>
  
  <p>Here's a subset of the data (1st 100 rows):
      <a href="https://drive.google.com/open?id=0By6o_bZ8CGwucVZGT0pBQlFzT2M" rel="nofollow noreferrer">https://drive.google.com/open?id=0By6o_bZ8CGwucVZGT0pBQlFzT2M</a></p>
  
  <p>Here's my script:</p>
  
  <pre><code>library(reshape2)
  library(dplyr)
  library(circular)
  
  #read in the long format data:
  wind &lt;- read.csv("&lt;INSERT_FILE_PATH_HERE&gt;", header = TRUE)
  
  #cast into wide format:
  wind.w &lt;- dcast(wind, 
              Local.Site.Name + Date.Local ~ Parameter.Name,
              value.var = "Arithmetic.Mean", 
              fun.aggregate = (
                if (wind$Parameter.Name == "Wind Direction - Resultant") {
                  mean(circular(wind$"Wind Direction - Resultant", units = 'degrees'))
                }
                else if (wind$Parameter.Name == "Wind Speed - Resultant") {
                  mean(wind$"Wind Speed - Resultant")
                }),
              na.rm = TRUE)
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>-spacedSparking</p>
  
  <p><strong>EDIT: HERE'S THE SOLUTION:</strong> </p>
  
  <pre><code>library(reshape2)
  library(SDMTools)
  library(dplyr)
  #read in the EPA wind data:
  #This data is publicly accessible, and can be found here: https://aqsdr1.epa.gov/aqsweb/aqstmp/airdata/download_files.html    
  wind &lt;- read.csv("daily_WIND_2016.csv", sep = ',', header = TRUE, stringsAsFactors = FALSE)
  
  #convert long format wind speed data by date and site id:
  wind_speed &lt;- dcast(wind, 
                      Local.Site.Name + Date.Local ~ Parameter.Name,
                      value.var = "Arithmetic.Mean",
                      fun.aggregate = function(x) {
                        mean(x, na.rm=TRUE)
                      },
                      subset = .(Parameter.Name == "Wind Speed - Resultant")
  )
  
  #convert long format wind direction data into wide format by date and local site id:
  wind_direction &lt;- dcast(wind, 
                          Local.Site.Name + Date.Local ~ Parameter.Name,
                          value.var = "Arithmetic.Mean",
                          fun.aggregate = function(x) {
                            if(length(x) &gt; 0) 
                              circular.averaging(x, deg = TRUE)
                            else
                              -1
                          },
                          subset= .(Parameter.Name == "Wind Direction - Resultant")
  )
  
  #join the wide format split wind_speed and wind_direction dataframes
  wind.w &lt;- merge(wind_speed, wind_direction)
  </code></pre>