Doerte Salecker
2007-Oct-26 18:21 UTC
[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
Bill.Venables at csiro.au
2007-Oct-27 02:20 UTC
[R] how do i find the annual maximun within several years?
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.
Bill.Venables at csiro.au
2007-Oct-27 02:37 UTC
[R] how do i find the annual maximun within several years?
Oops. It looks like your 'date' is not a true date object, but either a factor or a character string vector. You may have to change this a bit to Year <- function(date) as.POSIXlt(asDate(date, format = "%m/%d/%Y"))$year + 1900 M1 <- with(tab, tapply(measurement1, Year(date), max, na.rm = TRUE)) (I'm a little surprised you are using that date format, too. I thought the Americans were the only ones with that odd little quirk...) Bill Venables. -----Original Message----- From: Venables, Bill (CMIS, Cleveland) Sent: Saturday, 27 October 2007 12:20 PM To: 'Doerte Salecker'; 'r-help at stat.math.ethz.ch' Subject: RE: [R] how do i find the annual maximun within several years? 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.