[R] to extract extreme data for specified period

Jim Lemon jim at bitwrit.com.au
Tue Jun 17 14:35:52 CEST 2014


On Mon, 16 Jun 2014 11:51:44 PM dila radi wrote:
> Dear R users,
> 
> I used this codes to find the 1-day annual rainfall maximum
> 
> bmFort <- blockmaxxer(Fort, blocks = Fort$year, 
which="Prec");bmFort
> 
>  and the output is given below:
> 
> structure(list(obs = c(119, 507, 994, 1398, 1583, 1917), tobs = 
c(119,
> 142, 264, 303, 123, 91), month = c(4, 5, 9, 10, 5, 4), day = c(29,
> 22, 21, 30, 2, 1), year = c(1900, 1901, 1902, 1903, 1904, 1905
> ), Prec = c(2.39, 2.32, 4.34, 0.85, 3.02, 1.74)), .Names = c("obs",
> "tobs", "month", "day", "year", "Prec"), row.names = c(119L,
> 507L, 994L, 1398L, 1583L, 1917L), class = "data.frame")
> 
> however, I would also like to find the 7-days annual rainfall 
maximum by
> months within a year. I tried changing ' Fort$year' to ' Fort$day' and I
> got the following:
> 
> structure(list(obs = c(28610, 1583, 14125, 18052, 18905, 32937
> ), tobs = c(121, 123, 246, 155, 278, 65), month = c(5, 5, 9,
> 6, 10, 3), day = c(1, 2, 3, 4, 5, 6), year = c(1978, 1904, 1938,
> 1949, 1951, 1990), Prec = c(1.85, 3.02, 3.54, 3.54, 1.67, 3.48
> )), .Names = c("obs", "tobs", "month", "day", "year", "Prec"), 
row.names =
> c(28610L,
> 1583L, 14125L, 18052L, 18905L, 32937L), class = "data.frame")
> 
> what I want is the 7-days maximum within a year that is maximum 
value for
> eg. from 1/1/1990 to 7/1/1990 then another maximum is 8/1/1990 
to 14/1/1990
> and so on.
> 
> this is some sample data:
> structure(list(obs = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
> 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
> 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40), tobs = c(1,
> 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
> 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
> 36, 37, 38, 39, 40), month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 2, 2, 2, 2, 2, 2, 2, 2, 2), day = c(1, 2, 3, 4, 5, 6, 7, 8, 9,
> 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
> 26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9), year = c(1900,
> 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
> 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
> 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
> 1900, 1900, 1900, 1900, 1900, 1900), Prec = c(0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0.03, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0.08, 0.04, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0, 0)), .Names =
> c("obs",
> "tobs", "month", "day", "year", "Prec"), row.names = c(NA, 40L
> ), class = "data.frame")
> 
Hi Dila,
As you seem to want to divide the year into seven day intervals 
regardless of months, try this:

dr.df<-structure(list(obs = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40), tobs = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40), month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2), day = c(1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9), year = c(1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900), Prec = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0.03, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0.08, 0.04, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0, 0)), .Names =
c("obs",
"tobs", "month", "day", "year", "Prec"), row.names = c(NA, 40L
), class = "data.frame")
# create a grouping variable with the appropriate length
dr.df$byweek<-rep(1:52,each=7)[1:length(dr.df$Prec)]
# get the sum of each successive seven days
weeksums<-by(dr.df$Prec,dr.df$byweek,sum)

Then you can just get the max of weeksums.

Jim



More information about the R-help mailing list