[R] how do i find the annual maximun within several years?

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sat Oct 27 04:20:05 CEST 2007


Here's a possibility.

Year <- function(date) as.POSIXlt(date)$year + 1900 
M1 <- with(tab, tapply(measurement1, Year(date), max, na.rm = TRUE))

(always you have missing values that you wish to ignore...)

Pity about your busted shift key.  Keyboards are pretty cheap these
days, though...

Bill Venables.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Doerte Salecker
Sent: Saturday, 27 October 2007 4:21 AM
To: r-help at stat.math.ethz.ch
Subject: [R] how do i find the annual maximun within several years?

dear kind helper,

i would like to know how to find the annual maximun for a table that
basicly looks like this:

date		time		measurement1	measurement2
measurement3
mm/dd/yyyy	hh:mm:ss	m1		m2		m3

there are about 9000 measurements for each year, which makes it quite
large...
i already tried to subset all rows for a year, to find the maximum
within these choosen rows,

y <- grep(yyyy,table[,1],value=TRUE)
df <-table[with(table, date == y), ]
df[with(df, max(measurement1)), ]

which got result some years, but didn't show all rows within these
years, and for other years it didn't work at all, 

     V1   V2 V3 V4 V5
NA <NA> <NA> NA NA NA

or/and gave warnings like: 

Warning messages:
1: longer object length
        is not a multiple of shorter object length in: is.na(e1) |
is.na(e2)
2: longer object length
        is not a multiple of shorter object length in:
`==.default`(date, y) 

i also tried: 

result <- matrix(nrow=0,ncol=2)
for (y in yyyy:yyyy) {
  Y <- grep(y, table[,1],value=TRUE)
  Max <- max (subset(table, date %in% Y, c(date, measurement1))[,2])
  result2 <- (c(y,Max))
  result <- rbind(result2,result)
}
print(result)

that kind of worked, but only gave me the year in the result, but i need
the full date, for the statistical analysis. 
i hope you can help me with this,

thank you in advance,
d.salecker

______________________________________________
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