Hello,
Try the following.
Month <- format(Dat$open_date, "%m")
anom <- Dat$Dry_w - ave(Dat$Dry_w, Month, FUN = function(x) mean(x,
na.rm = TRUE))
st.anom <- Dat$PP_int - ave(Dat$PP_int, Month, FUN = function(x) mean(x,
na.rm = TRUE))
Hope this helps,
Rui Barradas
Em 18-02-2013 14:03, Hefri escreveu:>
>
>
>
> I have an irregular time series, which cannot easily be converted to a ts
> object (long stretches with NA).
>
> I have calculated the climatological mean and st.dev, but need help on how
> to calculate the anomalies from the climatology and my original data set.
> Below is an example, where I have indicated the basic idea of what I want
to
> do under Question 1 and 2. I assume I will have to create a custom function
> or use one of the apply functions, but I would greatly appreciate any help
> on how this can be achieved.
>
> # sample of data frame
>> dput(head(Dat, 10))
> structure(list(open_date = structure(c(11742, 11966, 11987, 12001,
> 12015, 12029, 12043, 12064, 12085, 12099), class = "Date"), Dry_w
= c(2.4,
> 14.05, 28.55, 20.76, 18.6, NA, 20.72, 18.47, 12.42, 25.57), PP_int = c(NA,
> 1281.52, 1352.15, 1333.05, 959.6, 738.24, 176.73, NA, 108.61,
> 518.42)), .Names = c("open_date", "Dry_w",
"PP_int"), row.names = 166:175,
> class = "data.frame")
>
> # monthly climatology: mean and st.dev for each calender month
>> (clim.m <- aggregate(Dat, by=list(Month=format(Dat$open_date,
"%m")),
>> mean, na.rm=TRUE))
> Month open_date Dry_w PP_int
> 1 01 2008-05-02 31.36200 299.9450
> 2 02 2006-11-09 21.94889 648.6460
> 3 03 2005-12-08 54.28700 454.7282
> 4 04 2005-09-11 87.05167 827.9433
> 5 05 2006-04-13 99.20818 961.3408
> 6 06 2007-07-19 99.98583 1203.0925
> 7 07 2007-06-24 72.55867 1561.2331
> 8 08 2007-06-11 48.58545 1750.0082
> 9 09 2006-11-20 74.86500 2443.2527
> 10 10 2006-12-25 45.15000 3466.7880
> 11 11 2006-03-14 30.59750 1938.0700
> 12 12 2007-02-27 24.38571 636.9043
>> (clim.sd <- aggregate(Dat, by=list(Month=format(Dat$open_date,
"%m")), sd,
>> na.rm=TRUE))
> Month open_date Dry_w PP_int
> 1 01 1225.5924 9.978272 196.2999
> 2 02 1258.2528 17.905547 447.2490
> 3 03 1202.0288 25.974303 163.2227
> 4 04 965.2724 67.163564 260.5524
> 5 05 1003.5452 57.804352 290.2006
> 6 06 828.4384 66.481851 384.5991
> 7 07 937.5725 45.718293 701.1342
> 8 08 907.5030 38.550746 513.8539
> 9 09 910.0279 81.506413 1532.9001
> 10 10 1180.1643 22.080882 2204.2142
> 11 11 1152.4069 15.250971 870.4910
> 12 12 1175.0547 14.899713 281.2347
>
> # Question 1, Anomalies (deviation from mean): subtracting climatological
> mean from observed data
> anom <- Dat - clim.m
>
> # Question 2, Standardized anomalies (or normalized anomalies): dividing
> monthly anomalies by the climatological st.dev
> st.anom <- anom / clim.sd
>
>
>
>
>
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Calculating-seasonal-anomalies-tp4658932.html
> Sent from the R help mailing list archive at Nabble.com.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>