title: How to calculate temperature departure from normal using a 30 year average - R 4.0.0
body: <p>I have added my entire code below, so what have I verified is working:</p>
  <ol>
  <li><p>I know for a fact I can download any climate station from the NCDC website for the specific time range I want. On a side note, if you can look at my 'bind_rows()` command and make it less messy, I could not find a better way to do it.</p>
  </li>
  <li><p>I know <code>TAVG</code> is calculated and working</p>
  </li>
  <li><p>Monthly summaries, which makes the data.set mso_sum works perfectly</p>
  </li>
  </ol>
  <p>So what is not working:</p>
  <ol>
  <li>Finding my departures from 30-year norms</li>
  </ol>
  <p>How do I want it to work:</p>
  <ol>
  <li>filter the years 1981:2010</li>
  <li>group so every January 1, Jan 2, Jan 3, etc, Feb 1, Feb 2, etc can be summarized by day</li>
  <li>summarize the mean TAVG (temperature average found from MaxT and MinT)</li>
  <li>then take the entire dataset and subtract daily TAVG from CliAvgT</li>
  </ol>
  <p>This is the code I tried:</p>
  <pre><code>mso_light %&gt;%
    group_by(month, day) %&gt;%
    summarise(CliAvgT = mean(TAVG[1981:2010], na.rm = T)) %&gt;%
    mutate(Avg_DepT = CliAvgT - TAVG) %&gt;%
    ungroup()
  </code></pre>
  <p>I also tried this alternate code:</p>
  <pre><code>mso_light %&gt;%
    filter(year &gt;= &quot;1981&quot;, year &lt;= &quot;2010&quot;) %&gt;%
    group_by(month, day) %&gt;%
    summarise(CliAvgT = mean(TAVG, na.rm = T)) %&gt;%
    mutate(Avg_DepT = CliAvgT - TAVG) %&gt;%
    ungroup()
  </code></pre>
  <p>and received this error message:</p>
  <pre><code>&gt; mso_light %&gt;%
  Warning messages:
  1: Unknown or uninitialised column: `value`. 
  2: Unknown or uninitialised column: `value`. 
  +   filter(year &gt;= &quot;1981&quot;, year &lt;= &quot;2010&quot;) %&gt;%
  +   group_by(month, day) %&gt;%
  +   summarise(CliAvgT = mean(TAVG, na.rm = T)) %&gt;%
  +   mutate(Avg_DepT = CliAvgT - TAVG) %&gt;%
  +   ungroup()
  `summarise()` regrouping output by 'month' (override with `.groups` argument)
  Error: Problem with `mutate()` input `Avg_DepT`.
  x object 'TAVG' not found
  i Input `Avg_DepT` is `CliAvgT - TAVG`.
  i The error occured in group 1: month = 1.
  Run `rlang::last_error()` to see where the error occurred.
  </code></pre>
  <p>Finally here is all my code:</p>
  <pre><code>library(rnoaa)
  library(tidyverse)
  library(data.table)
  library(&quot;openair&quot;)
  library(&quot;chron&quot;)
  library('lubridate')
  
  ## grab first half of year
  
  getNoaaP1 &lt;- function(yr, type = c('tmax','tmin','PRCP', 'SNOW', 'SNWD')) 
    ncdc(datasetid = 'GHCND', 
         stationid = 'GHCND:USW00024153',
         datatypeid = type, 
         startdate = paste0(yr, '-01-01'), 
         enddate = paste0(yr, '-06-30'), limit = 1000)  
  
  ## grab second half of year
  
  getNoaaP2 &lt;- function(yr, type = c('tmax','tmin','PRCP', 'SNOW', 'SNWD')) 
    ncdc(datasetid = 'GHCND', 
         stationid = 'GHCND:USW00024153',
         datatypeid = type, 
         startdate = paste0(yr, '-07-01'), 
         enddate = paste0(yr, '-12-31'), limit = 1000)  
  
  res1 &lt;- setNames(lapply(1948:2020, getNoaaP1), paste0(&quot;Year&quot;, 1948:2020, &quot;P1&quot;))
  res &lt;- setNames(lapply(1948:2020, getNoaaP2), paste0(&quot;Year&quot;, 1948:2020, &quot;P2&quot;))
  
  # this would export all individual list elements to the global environment:
  list2env(res, envir = .GlobalEnv) 
  list2env(res1, envir = .GlobalEnv) 
  
  # this would combine the individual lists
  mso &lt;- bind_rows(Year1948P1$data, Year1949P1$data, Year1950P1$data, Year1951P1$data,
                   Year1952P1$data, Year1953P1$data, Year1954P1$data, Year1955P1$data,
                   Year1956P1$data, Year1957P1$data, Year1958P1$data, Year1959P1$data,
                   Year1960P1$data, Year1961P1$data, Year1962P1$data, Year1963P1$data,
                   Year1964P1$data, Year1965P1$data, Year1966P1$data, Year1967P1$data,
                   Year1968P1$data, Year1969P1$data, Year1970P1$data, Year1971P1$data,
                   Year1972P1$data, Year1973P1$data, Year1974P1$data, Year1975P1$data,
                   Year1976P1$data, Year1977P1$data, Year1978P1$data, Year1979P1$data,
                   Year1980P1$data, Year1981P1$data, Year1982P1$data, Year1983P1$data,
                   Year1984P1$data, Year1985P1$data, Year1986P1$data, Year1987P1$data,
                   Year1988P1$data, Year1989P1$data, Year1990P1$data, Year1991P1$data,
                   Year1992P1$data, Year1993P1$data, Year1994P1$data, Year1995P1$data,
                   Year1996P1$data, Year1997P1$data, Year1998P1$data, Year1999P1$data,
                   Year2000P1$data, Year2001P1$data, Year2002P1$data, Year2003P1$data,
                   Year2004P1$data, Year2005P1$data, Year2006P1$data, Year2007P1$data,
                   Year2008P1$data, Year2009P1$data, Year2010P1$data, Year2011P1$data,
                   Year2012P1$data, Year2013P1$data, Year2014P1$data, Year2015P1$data,
                   Year2016P1$data, Year2017P1$data, Year2018P1$data, Year2019P1$data,
                   Year2020P1$data,
                   Year1948P2$data, Year1949P2$data, Year1950P2$data, Year1951P2$data,
                   Year1952P2$data, Year1953P2$data, Year1954P2$data, Year1955P2$data,
                   Year1956P2$data, Year1957P2$data, Year1958P2$data, Year1959P2$data,
                   Year1960P2$data, Year1961P2$data, Year1962P2$data, Year1963P2$data,
                   Year1964P2$data, Year1965P2$data, Year1966P2$data, Year1967P2$data,
                   Year1968P2$data, Year1969P2$data, Year1970P2$data, Year1971P2$data,
                   Year1972P2$data, Year1973P2$data, Year1974P2$data, Year1975P2$data,
                   Year1976P2$data, Year1977P2$data, Year1978P2$data, Year1979P2$data,
                   Year1980P2$data, Year1981P2$data, Year1982P2$data, Year1983P2$data,
                   Year1984P2$data, Year1985P2$data, Year1986P2$data, Year1987P2$data,
                   Year1988P2$data, Year1989P2$data, Year1990P2$data, Year1991P2$data,
                   Year1992P2$data, Year1993P2$data, Year1994P2$data, Year1995P2$data,
                   Year1996P2$data, Year1997P2$data, Year1998P2$data, Year1999P2$data,
                   Year2000P2$data, Year2001P2$data, Year2002P2$data, Year2003P2$data,
                   Year2004P2$data, Year2005P2$data, Year2006P2$data, Year2007P2$data,
                   Year2008P2$data, Year2009P2$data, Year2010P2$data, Year2011P2$data,
                   Year2012P2$data, Year2013P2$data, Year2014P2$data, Year2015P2$data,
                   Year2016P2$data, Year2017P2$data, Year2018P2$data, Year2019P2$data,
                   Year2020P2$data)
  
  ## build data.frame and remove 'station ID' column
  mso_light &lt;- mso[, -3]
  
  ## remove time from date group
  mso_date &lt;- mso_light[1]
  mso_date &lt;- sub(&quot;T.*&quot;, &quot;&quot;, mso_date$date)
  mso_light$date &lt;- mso_date 
  
  ## remove flags for fl_so? and fl_t (time)
  mso_light &lt;- mso_light[1:5]
  
  ## Change 'T' = 9998 &amp; 'M' = 9999
  mso_light$value[mso_light$fl_m == &quot;T&quot;] &lt;- 0
  mso_light$value[mso_light$fl_q == &quot;M&quot;] &lt;- 'na'
  
  mso_light$value &lt;- as.numeric(mso_light$value)
  
  ## pivot data frame
  
  ## eventually use to change column names
  ## v_names &lt;- c('PRCP', 'SNOW', 'SNWD', 'TMAX', 'TMIN')
  
  mso_light &lt;- mso_light[1:3]
  
  mso_light &lt;- pivot_wider(mso_light,
                            names_from = datatype,  
                            values_from = value)
  ## mso_light &lt;- select(mso_light, -c('fl_m','fl_q'))
  
  options(stringAsFactors = FALSE)
  
  mso_light$date &lt;- as.Date(mso_light$date, &quot;%Y-%m-%d&quot;)
  
  
  ## Turning all daily temperatures into an average
  
  mso_light &lt;- mso_light %&gt;% rowwise() %&gt;%
    mutate(TAVG = mean(c(TMAX, TMIN), na.rm = T))
  
  ## Composing daily data into monthly packages
  
  mso_light &lt;- mso_light %&gt;%
    mutate(month = month(date)) %&gt;%
    mutate(year = year(date)) %&gt;%
    mutate(day = day(date))
  
  mso_light &lt;- mso_light %&gt;%
    relocate('year', 'month', 'day') 
  
  ## mso_light &lt;- mso_light[-4]
  
  mso_sum &lt;- mso_light %&gt;%
    group_by(month, year) %&gt;% 
    summarize(AVG_TAVG=mean(TAVG, na.rm = TRUE),
            T_PRCP=sum(PRCP, na.rm=TRUE),
            T_SNOW=sum(SNOW, na.rm=TRUE)) %&gt;% 
    ungroup()
  
  ## make 30 year averages, using 1981-2010
  
  mso_light %&gt;%
    group_by(month, day) %&gt;%
    summarise(CliAvgT = mean(TAVG[1981:2010], na.rm = T)) %&gt;%
    mutate(Avg_DepT = CliAvgT - TAVG) %&gt;%
    ungroup()
  
  ##mso_DeptT &lt;- mso_light %&gt;%
  ##  group_by(month, day) %&gt;%
  ##  mean(mso_light$TAVG[1981:2010], na.rm = T) %&gt;%
  ##  ungroup()
  
  ##mso_DeptT &lt;- filter(mso_light, year &gt;= &quot;1981&quot;, year &lt;= &quot;2010&quot;) %&gt;%
  ##  group_by(day, month) %&gt;%
  ##  mutate(daily_DeptT = mean(TAVG, na.rm = T)) %&gt;%
  ##  ungroup()
  
  cli_Avg &lt;- filter(mso_sum, year &gt;= &quot;1981&quot;, year &lt;= &quot;2010&quot;) %&gt;%
    group_by(month) %&gt;%
    summarize(T_dep = mean(AVG_TAVG, na.rm = T),
              Mon_Precip = mean(T_PRCP, na.rm = T),
              Mon_Snow = mean(T_SNOW, na.rm = T))
  
  write.csv(mso_light, &quot;mso_light.csv&quot;)
  write.csv(mso_sum, &quot;mso_sum.csv&quot;)
  write.csv(cli_Avg, &quot;cli_avg.csv&quot;)
  </code></pre>