Reputation: 91
I have a matrix of daily data of average flow and want to make a summary matrix that shows the maximum peak flow. Here's a little sample of what my data looks like:
x<-c(5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
flow<-matrix(c(c(rep(1990,365),rep(1991,365),rep(1992,365)),sample(x,(365*3), replace=TRUE)),nrow=(365*3), ncol=2)
I'd like the summary matrix to be formatted with the year in column 1 and the peak flow event from that year in column 2. Here's an example of how I would like the summary matrix formatted.
summary=matrix(, ncol=2, nrow=3)
summary[,1]=c(1990,1991,1992)
Upvotes: 1
Views: 81
Reputation: 152
And the dplyr family of functions (building on @Bryans work):
DF <- as.data.frame(flow)
names(DF) <- c("year", "flow")
group_by(DF, year) %>% summarize(flow = max(flow))
Gives:
Source: local data frame [3 x 2]
year flow
1 1990 100
2 1991 100
3 1992 100
Upvotes: 1
Reputation: 6213
This should be close:
DF <- as.data.frame(flow)
names(DF) <- c("year", "flow")
DF$year <- as.factor(DF$year)
res <- aggregate(flow ~ year, data = DF, FUN = max)
And gives:
year flow
1 1990 100
2 1991 100
3 1992 100
in the form of a data frame.
Upvotes: 5