Reputation: 19
I have been working on a dataset that will be universal on multiple climate stations for analyzing temperature and precipitation. I have run into a brick wall designing the 'climatic norms', I have successfully calculated daily temperature average TAVG
, monthly temp avg AVG_TAVG
, and summed up PRCP
and SNOW
for monthly totals.
Where I am at a standstill is calculating departure from normal, currently, data from 1981 - 2010 is considered climate norms.
Here is what my dataset looks like currently:
mso_light
year month day date PRCP SNOW SNWD TMAX TMIN TAVG
1 1948 1 1 1948-01-01 0 0 102 44 -122 -39.0
2 1948 1 2 1948-01-02 3 0 51 44 6 25.0
3 1948 1 3 1948-01-03 0 0 25 44 -39 2.5
4 1948 1 4 1948-01-04 38 64 76 33 -56 -11.5
5 1948 1 5 1948-01-05 0 0 76 -6 -83 -44.5
6 1948 1 6 1948-01-06 107 0 51 22 -61 -19.5
7 1948 1 7 1948-01-07 147 0 25 28 -17 5.5
8 1948 1 8 1948-01-08 8 13 25 39 -83 -22.0
9 1948 1 9 1948-01-09 0 0 25 -6 -117 -61.5
10 1948 1 10 1948-01-10 8 10 25 -11 -156 -83.5
So I originally felt I needed date
for sorting purposes, I will remove it if not needed in the future.
Next, I would like to add a column for DepNormT
, which is calculated by taking every Jan 1 - Dec 31 from 1981 - 2010 and averaging TAVG
to find the normal average temp. Then DepNormT
will be the difference between itself and TAVG
for the entire dataset.
I have tried multiple ways to accomplish this here are two versions:
mso_DeptT <- mso_light %>%
group_by(month, day) %>%
mean(mso_light$TAVG[1981:2010], na.rm = T) %>%
ungroup()
This gives me the following error:
no applicable method for 'ungroup' applied to an object of class "c('double', 'numeric')"
In addition: Warning message:
In mean.default(., mso_light$TAVG[1981:2010], na.rm = T) :
argument is not numeric or logical: returning NA
This is another version:
##mso_DeptT <- filter(mso_light, year >= "1981", year <= "2010") %>%
## group_by(day, month) %>%
## mutate(daily_DeptT = mean(TAVG, na.rm = T)) %>%
## ungroup()
mso_sum <- mso_light %>%
group_by(month, year) %>%
summarize(AVG_TAVG=mean(TAVG, na.rm = TRUE),
T_PRCP=sum(PRCP, na.rm=TRUE),
T_SNOW=sum(SNOW, na.rm=TRUE)) %>%
ungroup()
## To find monthly normal precipitation and snowfall - using dataset mso_sum
cli_Avg <- filter(mso_sum, year >= "1981", year <= "2010") %>%
group_by(month) %>%
summarize(Mon_Precip = mean(T_PRCP, na.rm = T),
Mon_Snow = mean(T_SNOW, na.rm = T))
This gave me a 30 year average that was equal to each individual day average TAVG
. For example:
year month day date PRCP SNOW SNWD TMAX TMIN TAVG DepNormT
1 1948 1 1 1948-01-01 0 0 102 44 -122 -39.0 -39.0
2 1948 1 2 1948-01-02 3 0 51 44 6 25.0 25.0
3 1948 1 3 1948-01-03 0 0 25 44 -39 2.5 2.5
4 1948 1 4 1948-01-04 38 64 76 33 -56 -11.5 ect
5 1948 1 5 1948-01-05 0 0 76 -6 -83 -44.5 .
6 1948 1 6 1948-01-06 107 0 51 22 -61 -19.5 .
7 1948 1 7 1948-01-07 147 0 25 28 -17 5.5 .
8 1948 1 8 1948-01-08 8 13 25 39 -83 -22.0
9 1948 1 9 1948-01-09 0 0 25 -6 -117 -61.5
10 1948 1 10 1948-01-10 8 10 25 -11 -156 -83.5
Thanks for suggestions.
Upvotes: 2
Views: 65