简体   繁体   English

计算满足条件的连续天数的平均值

[英]Calculating mean over consecutive days that meet a condition

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我的数据的标题(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 . 2005 年 7 月 3 2005/7/32005/7/42005/7/3月 4 2005/7/3 2005/7/4最高为35.6 ,其中 2005 年2005/7/72005/7/102005/7/7并于 2005 年2005/7/10月 10 2005/7/7结束的第二次热浪的最高平均值为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 :说明

  • Filter the data for only the hot days: hotday == 1 (or with some code-golfing: !!hotday ).仅过滤热天的数据: hotday == 1 (或使用一些代码打高尔夫球: !!hotday )。
  • For the remaining rows, create a runlength id with the rleid -function so that you can group by heatwave: rleid(hw.length) .对于剩余的行,使用rleid函数创建一个运行长度 ID,以便您可以按热浪分组: rleid(hw.length)
  • Finally extract the maximum mean for each heat wave and assign it to a new colum with: maxmean := max(MEAN) .最后提取每个热浪的最大平均值并将其分配给一个新的列: 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:我不知道你的代码的详细情况,但这里有一个提示,假设 Period1 是一个数据帧:

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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM