I'm using a cods where I can detect heat wave for consecutive days, but I cannot figure how I can also get the cods select the highest mean temperature recorded over every heat weave event. The header of my data (Period1): Year Month Day Mean MAX MIN
My codes are:
setDT(Period1)
Period1[, hotday := +(Period1$MAX>=(quantile(Period1$MAX,.9, na.rm = T, type = 6)) & Period1$MIN>=(quantile(Period1$MIN,.8, na.rm = T, type = 6)))
][, hw.length := with(rle(hotday), rep(lengths,lengths))
][hotday==0, hw.length:=0]
The result of the code:
YEAR MONTH DAY MEAN MAX MIN hotday hw.length
1 2005 7 1 34.7 42 28 0 0
2 2005 7 2 35 41.8 28.8 0 0
3 2005 7 3 34.6 41.5 27 1 2
4 2005 7 4 35.4 43 27.6 1 2
5 2005 7 5 35.4 42 27.4 0 0
6 2005 7 6 34.4 42.2 27.7 0 0
7 2005 7 7 34 39.3 28.6 1 4
8 2005 7 8 34.9 40.6 29 1 4
9 2005 7 9 35.7 41 28.2 1 4
10 2005 7 10 35.1 42.2 28.5 1 4
11 2005 7 11 34.9 42.5 29 0 0
12 2005 7 12 35.5 43.2 28 0 0
13 2005 7 13 36.1 43 29.2 1 3
14 2005 7 14 36.4 43.4 32 1 3
15 2005 7 15 36.5 44.5 29.2 1 3
16 2005 7 16 36.2 42.6 31.2 0 0
17 2005 7 17 34.6 40 30 0 0
18 2005 7 18 33.7 41 28.8 0 0
For heat wave on 2005/7/3
and 2005/7/4
the highest is 35.6
, where for the second heat wave that stared on 2005/7/7
and ended on 2005/7/10
the highest mean was 35.7
. I would appreciate any idea on how I can include extracting the highest Mean of every heat wave considering the consecutive days.
With:
Period1[hotday == 1, maxmean := max(MEAN) , rleid(hw.length)][]
you get:
YEAR MONTH DAY MEAN MAX MIN hotday hw.length maxmean
1: 2005 7 1 34.7 42.0 28.0 0 0 NA
2: 2005 7 2 35.0 41.8 28.8 0 0 NA
3: 2005 7 3 34.6 41.5 27.0 1 2 35.4
4: 2005 7 4 35.4 43.0 27.6 1 2 35.4
5: 2005 7 5 35.4 42.0 27.4 0 0 NA
6: 2005 7 6 34.4 42.2 27.7 0 0 NA
7: 2005 7 7 34.0 39.3 28.6 1 4 35.7
8: 2005 7 8 34.9 40.6 29.0 1 4 35.7
9: 2005 7 9 35.7 41.0 28.2 1 4 35.7
10: 2005 7 10 35.1 42.2 28.5 1 4 35.7
11: 2005 7 11 34.9 42.5 29.0 0 0 NA
12: 2005 7 12 35.5 43.2 28.0 0 0 NA
13: 2005 7 13 36.1 43.0 29.2 1 3 36.5
14: 2005 7 14 36.4 43.4 32.0 1 3 36.5
15: 2005 7 15 36.5 44.5 29.2 1 3 36.5
16: 2005 7 16 36.2 42.6 31.2 0 0 NA
17: 2005 7 17 34.6 40.0 30.0 0 0 NA
18: 2005 7 18 33.7 41.0 28.8 0 0 NA
Explanation :
hotday == 1
(or with some code-golfing: !!hotday
).rleid
-function so that you can group by heatwave: rleid(hw.length)
.maxmean := max(MEAN)
.If you just want to extract the maximum values for the heat waves, you can use:
> Period1[!!hotday, max(MEAN) , rleid(hw.length)]$V1
[1] 35.4 35.7 36.5
Used data:
Period1 <- fread('YEAR MONTH DAY MEAN MAX MIN hotday hw.length
2005 7 1 34.7 42 28 0 0
2005 7 2 35 41.8 28.8 0 0
2005 7 3 34.6 41.5 27 1 2
2005 7 4 35.4 43 27.6 1 2
2005 7 5 35.4 42 27.4 0 0
2005 7 6 34.4 42.2 27.7 0 0
2005 7 7 34 39.3 28.6 1 4
2005 7 8 34.9 40.6 29 1 4
2005 7 9 35.7 41 28.2 1 4
2005 7 10 35.1 42.2 28.5 1 4
2005 7 11 34.9 42.5 29 0 0
2005 7 12 35.5 43.2 28 0 0
2005 7 13 36.1 43 29.2 1 3
2005 7 14 36.4 43.4 32 1 3
2005 7 15 36.5 44.5 29.2 1 3
2005 7 16 36.2 42.6 31.2 0 0
2005 7 17 34.6 40 30 0 0
2005 7 18 33.7 41 28.8 0 0')
I don't know exactly how your code in details look like, but here is a hint assuming Period1 is a dataframe:
Extract the hot period
hotPeriod <- Period1[, hotday := +(Period1$MAX>=(quantile(Period1$MAX,.9, na.rm = T, type = 6)) & Period1$MIN>=(quantile(Period1$MIN,.8, na.rm = T, type = 6)))
][, hw.length := with(rle(hotday), rep(lengths,lengths))
][hotday==0, hw.length:=0]
Extract the maximum of the mean value over that period
max.of.mean <- max( hotPeriod$Mean )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.