[R] finding the annual maximun within several years
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Nov 13 13:09:45 CET 2007
We can do this using chron and zoo. First turn it into
a zoo series, z, using chron times, tt, which may not only
be useful for this but also for other purposes.
The ave(...) command gets a vector as long as the series with
the minimum dates in year each and then we aggregate z
by that using max.
library(chron)
library(zoo)
tt <- chron(DF$V1, DF$V2)
z <- zoo(DF$V4, tt)
aggregate(z, ave(dates(tt), years(tt), FUN = min), max)
Not sure what you meant by "and so on" but if you just
meant you want the maximum of each of V3, V4 and V5 then
by year then replace the "z<-..." line above with
z <- zoo(data.matrix(DF[3:5]), tt)
in the above.
See
library(zoo)
vignette("zoo")
vignette("zoo-quickref")
for more on zoo and see the help desk article in R News 4/1 for
more on dates.
On Nov 13, 2007 2:44 AM, "Dörte Salecker" <15_kamele_fuer_aishe at gmx.net> wrote:
> dear r-helpers
>
> i've got a table that in extracts looks like this:
>
> V1 V2 V3 V4 V5
> 1 01/01/1975 00:00:00 125.837 3.691 296.618
> 2 01/01/1975 01:00:00 124.799 3.679 281.307
> 3 01/01/1975 02:00:00 111.607 3.536 281.307
> 4 02/24/1976 11:00:00 21.602 2.555 93.893
> 5 02/24/1976 12:00:00 27.804 2.623 93.893
> 6 02/24/1976 13:00:00 26.105 2.604 114.716
> 7 10/18/1977 00:00:00 18.850 2.525 330.203
> 8 10/18/1977 01:00:00 21.561 2.555 320.465
> 9 10/18/1977 02:00:00 21.941 2.559 314.505
> 10 11/05/1978 01:00:00 24.981 2.592 310.464
> 11 11/05/1978 02:00:00 19.066 2.528 310.464
> 12 11/05/1978 03:00:00 26.198 2.605 305.749
> 13 06/29/1979 19:00:00 78.829 3.179 306.199
> 14 06/29/1979 20:00:00 92.221 3.325 306.199
> 15 06/29/1979 21:00:00 72.364 3.108 303.049
>
> i need to find the annual maximun for each year in V3, V4 and V5 So that the result would be a table like this:
>
> V1 V4
> 1 01/01/1975 3.691
> 2 02/24/1976 2.623
> 3 10/18/1977 2.559
> .
> .
> .and so on.
>
> i hope you can help me with this,
>
> thank you in advance,
> doerte
>
> --
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list