"Dörte Salecker"
2007-Nov-13 07:44 UTC
[R] finding the annual maximun within several years
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 --
Prof Brian Ripley
2007-Nov-13 08:32 UTC
[R] finding the annual maximun within several years
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
Gabor Grothendieck
2007-Nov-13 12:09 UTC
[R] finding the annual maximun within several years
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. >