[R] finding the annual maximun within several years

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 13 09:32:10 CET 2007


I presume you want this separately for each of V3, V4 and V5,
and illustrated V4?

Let's first create a new column for the year.  If the table is 'tab':

tab$year <- substring(tab$V1, 7)

Then by() almost does the job, but we need to reformat the result.

myfun <- function(tab, col) {
     i <- which.max(tab[[col]])[1]
     tab[i, c("V1", col)]
}
do.call("rbind", by(tab, tab$year, myfun, col="V4"))

which gives

              V1    V4
1975 01/01/1975 3.691
1976 02/24/1976 2.623
1977 10/18/1977 2.559
1978 11/05/1978 2.605
1979 06/29/1979 3.325

on your example.


On Tue, 13 Nov 2007, "Dörte Salecker" 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

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list