How to get the date of maximum values of rainfall in programming language R
I have a data frame with an year of daily values of rainfall (complete dates in column 1,months in column 2, rainfall in column 3). I am trying to calcu开发者_运维百科late monthly maximum rainfall and I also would like to know the date when the maximum occurred.
I tried the following code:
for (imonth in 1:12) {
month <- which(data[,2]==imonth)
monthly_max[imonth] <- max(data[month,3])
maxi[imonth] <- which.max(data[month,3])
}
tabela <- cbind(monthly_max, maxi)
write.table(tabela, col.names=TRUE, row.names=TRUE, append=FALSE, sep="\t")
The monthly maximum worked perfectly but the which.max function is not working correctly. Is giving me rows that do not correspond to the maximum values of rainfall. Can anybody tell me why or maybe suggest a better way of doing this?
Thank you for helping!
Here is a possible solution using the plyr package
library(plyr)
# create a dummy data frame
df = data.frame(date = sample(LETTERS, 100, replace = T),
month = sample(12, 100, replace = T),
rainfall = sample(1000, 100, replace = F));
# use plyr to figure out max rainfall and date for each month
df.max = ddply(df, .(month), summarize,
max.rain = max(rainfall),
date.max.rain = date[which.max(rainfall)])
Let me know if this works.
EDIT. If there are multiple dates with max rainfall, the code needs to be modified slightly
# find max rainfall for each month
df.max = ddply(df, .(month), transform, max.rain = max(rainfall))
# extract subset such that max.rain = rainfall
df.max = subset(df.max, max.rain == rainfall)
The index function works well here:
library(zoo)
data(AirPassengers)
APZ = zoo(AirPassengers)
ndx = which.max(APZ)
dmax = index(APZ[ndx])
# returns '1960.5' which is Jul 1960 once you know the series freq
frequency(APZ)
# returns 12
I have assumed that you are working with a timeseries object; for those (objects created using eg, ts, zooreg, xts) the dates are actually the value indices. If instead you have a dataframe (ie, so that date is a column in the data frame and the value is another column) then you can just access the row directly.
Edit in light of OP's comment below. For data stored as a data frame:
Suppose your data looks like this, a data frame, D0:
D0[1:10,]
# returns
Time Value
1 2011-03-12 10:48:24 -3.077784
2 2011-03-12 10:49:24 -20.145500
3 2011-03-12 10:50:24 -45.047560
4 2011-03-12 10:51:24 -69.949640
5 2011-03-12 10:52:24 -94.571920
6 2011-03-12 10:53:24 -112.199200
7 2011-03-12 10:54:24 -118.914400
8 2011-03-12 10:55:24 -114.997200
9 2011-03-12 10:56:24 -97.369900
10 2011-03-12 10:57:24 -78.063800
ndx = which.max(D0$Value)
dmax = D0[ndx,] # dmax gives the date corresponding to the max value
精彩评论