title: Average columns in a dataframe in R
body: <p>I would like to average the columns in a dataframe in R that contains integer values and occasionally NA's.</p>
  
  <p>The dataframe called CD6 (Climate Division 6) which is initialized with NA values to store the average values for all data belonging to Climate Division 6.  The rows are dates and the columns represent hours from 0 to 23.  The dataframe looks like this:</p>
  
  <pre><code>    &gt; CD6
  
         Date       H0 H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 ... H23
         1948-07-01 NA NA NA NA NA NA NA NA NA NA NA  ... NA
         1948-07-02 NA NA NA NA NA NA NA NA NA NA NA  ... NA
         1948-07-03 NA NA NA NA NA NA NA NA NA NA NA  ... NA
  </code></pre>
  
  <p>The dataframe called CA has the true values for all climate divisions ranging from 1 to 7.  The data frame looks something like this:</p>
  
  <pre><code>    &gt; CA
  
         Climate_Division  Date       H0 H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 ... H23
         6                 1948-07-01 NA NA NA NA NA NA NA NA NA NA NA  ... NA
         5                 1948-07-01 0  1  1  3  0  0  0  0  0  0  0   ... 2
         6                 1948-07-01 0  1  1  3  0  0  0  0  0  0  0   ... 2
         6                 1948-07-01 1  0  0  5  7  0  1  1  1  0  0   ... 0
         6                 1948-07-02 0  2  1  2  1  1  NA 0  1  0  1  ... 2
         6                 1948-07-03 NA NA NA NA NA NA NA NA NA NA NA  ... NA
  </code></pre>
  
  <p>I have a for loop coded that will iterate through the dataframe CA row by row and map out to the correct dataframe for the climate division (in this example CD6 for climate division 6).  A problem is that I don't know how many rows there are that belong to each climate division to properly take the average of it.  </p>
  
  <p>By looking only at CD6, I would like to obtain the average for each date at particular hour that disregards NA if true values are present and that the final answer is a whole number (ceiling of the value).  If all the hours at the various climate division have a value of NA, I would like to keep it as so as oppose to 0.  The end result should look like this for CD6</p>
  
  <pre><code>    &gt; CD6
  
         Date       H0 H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 ... H23
         1948-07-01 1  1  1  4  4  0  1  1  1  0  0   ... 1
         1948-07-02 0  2  1  2  1  1  NA 0  1  0  1  ... 2
         1948-07-03 NA NA NA NA NA NA NA NA NA NA NA  ... NA
  </code></pre>
  
  <p>I don't know exactly how to go about this in coding it and having it proficient.  So any suggestions will be helpful and thank for your time.</p>