从 R 中的 rle() 计算连续几天的第一次出现
Count first occurrence of consecutive days from rle() in R
我有以下数据:
dat <- structure(list(Year = c(1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1987L,
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L,
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L,
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L,
1987L, 1987L, 1987L), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
Day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L), Rainfall = c(1, 35.5, 20.3, 2.5,
32, 66.8, 0, 0, 1.8, 0, 5.3, 0, 0, 0, 11.7, 40.4, 45.7, 15.3,
21.6, 10.5, 26.2, 54.1, 1.5, 26.9, 39.4, 21.6, 1.3, 95.6,
10.2, 0, 5.1, 9.1, 31.5, 24.6, 18.5, 0, 37.1, 111.3, 4.3,
21.1, 3.1, 0, 0, 0, 15.8, 30.7, 6.4, 68.6, 97.5, 64.3, 47.3,
0, 2.3, 8.7, 53.9, 6.9, 20.9, 94, 7.4, 0, 1.3, 0, 38.6, 2.5,
10.4, 22.6, 6.3, 20.2, 3.6, 30.8, 0.8, 0.5, 34.3, 315.9,
246.3, 131.1, 11.8, 4.9, 2.1, 0.6, 0, 0, 0.5, 0.3, 122.2,
85.9, 40.2, 9.4, 15.4, 27.7, 2.3, 41.1, 248.9, 0.8, 0, 9.3,
10.9, 6.4, 0.3, 0.8, 0, 56.2, 150.6, 80, 12.2, 3.3, 0, 10.4,
95.3, 53, 55.9, 6, 46.2, 19.8, 1, 1.5, 10, 12, 47.8, 10.9,
80.5, 144.5, 250.6, 17.1, 1, 0, 15, 7.3, 69, 14.2, 6.6, 4.1,
0, 0.8, 2.5, 6.1, 44.5, 0, 23.1, 0.5, 2.5, 11.2, 5.4, 6.1,
0.5, 0, 0, 0, 0, 3.8, 0, 15.6, 16.3, 0.8, 1.3, 88.7, 18.3,
0.9, 1.3, 1.5, 0.5, 0.6, 5.3, 60.2, 0, 0.8, 19.1, 83.8, 62.7,
51.6, 3.3, 33.8, 7.6, 8.1, 0, 0, 16.3, 19, 28.2, 35.6, 2.9,
1.5, 17.8, 38.8, 15.8, 18.8, 203.7, 13.5, 30, 129.8, 5.6,
2, 0, 0, 0, 0, 0, 0, 0, 1.3, 0, 1, 2.5, 0, 3.8, 9.4, 1, 0,
24.6, 11.4, 11.7, 11.4, 0, 0, 0, 2.3, 8.6, 3, 14.4, 20.4,
4.2, 1.4, 20.8, 58.4, 42.9, 45.4, 67.6, 19.5, 4, 8, 24.9,
6.9, 75, 52.2, 110.4, 76.1, 10.6, 44.6, 248, 119.2, 1.2,
2, 9.5, 4.6, 29, 5.7, 49.6, 0, 0, 3, 22.4, 3, 3.2, 0, 26.2,
11, 8.8, 0, 4.2, 2, 9.4, 23.6, 76.8, 1, 60.6, 25.4, 6.2,
15.9, 9.2, 2, 0, 23.2, 22.8, 15, 4.2, 10.5, 2.8, 6, 26)), na.action = structure(c(`2000` =
2000L,
`2007` = 2007L, `3515` = 3515L, `3653` = 3653L, `10318` = 10318L,
`10319` = 10319L, `10320` = 10320L, `10321` = 10321L, `10322` = 10322L,
`10323` = 10323L, `10324` = 10324L, `10325` = 10325L, `10326` = 10326L,
`10327` = 10327L, `10328` = 10328L, `10329` = 10329L, `10330` = 10330L,
`10331` = 10331L, `10332` = 10332L, `10333` = 10333L, `10334` = 10334L,
`10335` = 10335L, `10336` = 10336L, `10337` = 10337L, `10338` = 10338L,
`10339` = 10339L, `10340` = 10340L, `10341` = 10341L, `10342` = 10342L,
`10343` = 10343L, `10344` = 10344L, `10345` = 10345L, `10346` = 10346L,
`10347` = 10347L, `10501` = 10501L, `10502` = 10502L, `10503` = 10503L,
`10504` = 10504L, `10505` = 10505L, `10506` = 10506L, `10507` = 10507L,
`10508` = 10508L, `10509` = 10509L, `10510` = 10510L, `10511` = 10511L,
`10512` = 10512L, `10513` = 10513L, `10514` = 10514L, `10515` = 10515L,
`10516` = 10516L, `10517` = 10517L, `10518` = 10518L, `10519` = 10519L,
`10520` = 10520L, `10521` = 10521L, `10522` = 10522L, `10523` = 10523L,
`10524` = 10524L, `10525` = 10525L, `10526` = 10526L, `10527` = 10527L,
`10528` = 10528L, `10529` = 10529L, `10530` = 10530L, `10531` = 10531L,
`10593` = 10593L, `10594` = 10594L, `10595` = 10595L, `10596` = 10596L,
`10597` = 10597L, `10598` = 10598L, `10599` = 10599L, `10600` = 10600L,
`10601` = 10601L, `10602` = 10602L, `10603` = 10603L, `10604` = 10604L,
`10605` = 10605L, `10606` = 10606L, `10607` = 10607L, `10608` = 10608L,
`10609` = 10609L, `10610` = 10610L, `10611` = 10611L, `10612` = 10612L,
`10613` = 10613L, `10614` = 10614L, `10615` = 10615L, `10616` = 10616L,
`10617` = 10617L, `10618` = 10618L, `10619` = 10619L, `10620` = 10620L,
`10621` = 10621L, `10622` = 10622L, `10623` = 10623L, `10624` = 10624L,
`10625` = 10625L, `10626` = 10626L, `10627` = 10627L, `10628` = 10628L,
`10629` = 10629L, `10630` = 10630L, `10631` = 10631L, `10632` = 10632L,
`10633` = 10633L, `10634` = 10634L, `10635` = 10635L, `10636` = 10636L,
`10637` = 10637L, `10638` = 10638L, `10639` = 10639L, `10640` = 10640L,
`10641` = 10641L, `10642` = 10642L, `10643` = 10643L, `10644` = 10644L,
`10645` = 10645L, `10646` = 10646L, `10647` = 10647L, `10648` = 10648L,
`10649` = 10649L, `10650` = 10650L, `10651` = 10651L, `10652` = 10652L,
`10653` = 10653L, `10654` = 10654L, `10655` = 10655L, `10656` = 10656L,
`10657` = 10657L, `10658` = 10658L, `10659` = 10659L, `10660` = 10660L,
`10661` = 10661L, `10662` = 10662L, `10663` = 10663L, `10664` = 10664L,
`10665` = 10665L, `10666` = 10666L, `10667` = 10667L, `10668` = 10668L,
`10669` = 10669L, `10670` = 10670L, `10671` = 10671L, `10672` = 10672L,
`10673` = 10673L, `10674` = 10674L, `10675` = 10675L, `10676` = 10676L,
`10677` = 10677L, `10678` = 10678L, `10679` = 10679L, `10680` = 10680L,
`10681` = 10681L, `10682` = 10682L, `10683` = 10683L, `10775` = 10775L,
`10776` = 10776L, `10777` = 10777L, `10778` = 10778L, `10779` = 10779L,
`10780` = 10780L, `10781` = 10781L, `10782` = 10782L, `10783` = 10783L,
`10784` = 10784L, `10785` = 10785L, `10786` = 10786L, `10787` = 10787L,
`10788` = 10788L, `10789` = 10789L, `10790` = 10790L, `10791` = 10791L,
`10792` = 10792L, `10793` = 10793L, `10794` = 10794L, `10795` = 10795L,
`10796` = 10796L, `10797` = 10797L, `10798` = 10798L, `10799` = 10799L,
`10800` = 10800L, `10801` = 10801L, `10802` = 10802L, `10803` = 10803L,
`10804` = 10804L, `10805` = 10805L, `10928` = 10928L, `10929` = 10929L,
`10930` = 10930L, `10931` = 10931L, `10932` = 10932L, `10933` = 10933L,
`10934` = 10934L, `10935` = 10935L, `10936` = 10936L, `10937` = 10937L,
`10938` = 10938L, `10939` = 10939L, `10940` = 10940L, `10941` = 10941L,
`10942` = 10942L, `10943` = 10943L, `10944` = 10944L, `10945` = 10945L,
`10946` = 10946L, `10947` = 10947L, `10948` = 10948L, `10949` = 10949L,
`10950` = 10950L, `10951` = 10951L, `10952` = 10952L, `10953` = 10953L,
`10954` = 10954L, `10955` = 10955L, `10956` = 10956L, `10957` = 10957L,
`10958` = 10958L, `11306` = 11306L, `11307` = 11307L, `11308` = 11308L,
`11309` = 11309L, `11310` = 11310L, `11311` = 11311L, `11312` = 11312L,
`11444` = 11444L, `11445` = 11445L, `11446` = 11446L, `11447` = 11447L,
`11448` = 11448L, `11449` = 11449L, `11450` = 11450L, `11451` = 11451L,
`11452` = 11452L, `11453` = 11453L, `11454` = 11454L, `11455` = 11455L,
`11456` = 11456L, `11457` = 11457L, `11458` = 11458L, `11459` = 11459L,
`11460` = 11460L, `11461` = 11461L, `11462` = 11462L, `11463` = 11463L,
`11464` = 11464L, `11465` = 11465L, `11466` = 11466L, `11467` = 11467L,
`11468` = 11468L, `11469` = 11469L, `11470` = 11470L, `11471` = 11471L,
`11472` = 11472L, `11473` = 11473L, `11474` = 11474L, `11658` = 11658L,
`11659` = 11659L, `11660` = 11660L, `11661` = 11661L, `11662` = 11662L,
`11663` = 11663L, `11664` = 11664L, `11665` = 11665L, `11666` = 11666L,
`11667` = 11667L, `11668` = 11668L, `11669` = 11669L, `11670` = 11670L,
`11671` = 11671L, `11672` = 11672L, `11673` = 11673L, `11674` = 11674L,
`11675` = 11675L, `11676` = 11676L, `11677` = 11677L, `11678` = 11678L,
`11679` = 11679L, `11680` = 11680L, `11681` = 11681L, `11682` = 11682L,
`11683` = 11683L, `11684` = 11684L, `11685` = 11685L, `11686` = 11686L,
`11687` = 11687L, `11688` = 11688L, `12389` = 12389L, `12390` = 12390L,
`12391` = 12391L, `12392` = 12392L, `12393` = 12393L, `12394` = 12394L,
`12395` = 12395L, `12396` = 12396L, `12397` = 12397L, `12398` = 12398L,
`12399` = 12399L, `12400` = 12400L, `12401` = 12401L, `12402` = 12402L,
`12403` = 12403L, `12404` = 12404L, `12405` = 12405L, `12406` = 12406L,
`12407` = 12407L, `12408` = 12408L, `12409` = 12409L, `12410` = 12410L,
`12411` = 12411L, `12412` = 12412L, `12413` = 12413L, `12414` = 12414L,
`12415` = 12415L, `12416` = 12416L, `12417` = 12417L, `12418` = 12418L,
`12419` = 12419L, `13270` = 13270L, `13271` = 13271L, `13272` = 13272L,
`13273` = 13273L, `13274` = 13274L, `13275` = 13275L, `13276` = 13276L,
`13277` = 13277L, `13278` = 13278L, `13279` = 13279L, `13280` = 13280L,
`13281` = 13281L, `13282` = 13282L, `13283` = 13283L, `13284` = 13284L,
`13285` = 13285L, `13286` = 13286L, `13287` = 13287L, `13288` = 13288L,
`13289` = 13289L, `13290` = 13290L, `13291` = 13291L, `13292` = 13292L,
`13293` = 13293L, `13294` = 13294L, `13295` = 13295L, `13296` = 13296L,
`13297` = 13297L, `13298` = 13298L, `13299` = 13299L, `13300` = 13300L,
`13362` = 13362L, `13363` = 13363L, `13364` = 13364L, `13365` = 13365L,
`13366` = 13366L, `13367` = 13367L, `13368` = 13368L, `13369` = 13369L,
`13370` = 13370L, `13371` = 13371L, `13372` = 13372L, `13373` = 13373L,
`13374` = 13374L, `13375` = 13375L, `13376` = 13376L, `13377` = 13377L,
`13378` = 13378L, `13379` = 13379L, `13380` = 13380L, `13381` = 13381L,
`13382` = 13382L, `13383` = 13383L, `13384` = 13384L, `13385` = 13385L,
`13386` = 13386L, `13387` = 13387L, `13388` = 13388L, `13389` = 13389L,
`13390` = 13390L, `13391` = 13391L, `13392` = 13392L, `14050` = 14050L
), class = "omit"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 366L,
367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L, 377L,
378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L, 388L,
389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L, 732L, 733L, 734L,
735L, 736L, 737L, 738L, 739L, 740L, 741L, 742L, 743L, 744L, 745L,
746L, 747L, 748L, 749L, 750L, 751L, 752L, 753L, 754L, 755L, 756L,
757L, 758L, 759L, 760L, 761L, 762L, 1097L, 1098L, 1099L, 1100L,
1101L, 1102L, 1103L, 1104L, 1105L, 1106L, 1107L, 1108L, 1109L,
1110L, 1111L, 1112L, 1113L, 1114L, 1115L, 1116L, 1117L, 1118L,
1119L, 1120L, 1121L, 1122L, 1123L, 1124L, 1125L, 1126L, 1127L,
1462L, 1463L, 1464L, 1465L, 1466L, 1467L, 1468L, 1469L, 1470L,
1471L, 1472L, 1473L, 1474L, 1475L, 1476L, 1477L, 1478L, 1479L,
1480L, 1481L, 1482L, 1483L, 1484L, 1485L, 1486L, 1487L, 1488L,
1489L, 1490L, 1491L, 1492L, 1827L, 1828L, 1829L, 1830L, 1831L,
1832L, 1833L, 1834L, 1835L, 1836L, 1837L, 1838L, 1839L, 1840L,
1841L, 1842L, 1843L, 1844L, 1845L, 1846L, 1847L, 1848L, 1849L,
1850L, 1851L, 1852L, 1853L, 1854L, 1855L, 1856L, 1857L, 2193L,
2194L, 2195L, 2196L, 2197L, 2198L, 2199L, 2200L, 2201L, 2202L,
2203L, 2204L, 2205L, 2206L, 2207L, 2208L, 2209L, 2210L, 2211L,
2212L, 2213L, 2214L, 2215L, 2216L, 2217L, 2218L, 2219L, 2220L,
2221L, 2222L, 2223L, 2558L, 2559L, 2560L, 2561L, 2562L, 2563L,
2564L, 2565L, 2566L, 2567L, 2568L, 2569L, 2570L, 2571L, 2572L,
2573L, 2574L, 2575L, 2576L, 2577L, 2578L, 2579L, 2580L, 2581L,
2582L, 2583L, 2584L, 2585L, 2586L, 2587L, 2588L, 2923L, 2924L,
2925L, 2926L, 2927L, 2928L, 2929L, 2930L, 2931L, 2932L, 2933L,
2934L, 2935L, 2936L, 2937L, 2938L, 2939L, 2940L, 2941L, 2942L,
2943L, 2944L, 2945L, 2946L, 2947L, 2948L, 2949L, 2950L, 2951L,
2952L, 2953L), class = "data.frame")
我正在计算超过第 95 个百分位数的天数,持续时间为 1 天、2 天等。
这是脚本:
jan %>%
group_by(Year,Month) %>%
mutate(extreme = Rainfall > quantile(Rainfall,0.95,na.rm=TRUE),
grp = data.table::rleid(extreme)) %>%
filter(extreme) %>%
add_count(grp) %>%
ungroup %>%
select(-extreme, -grp) %>%
group_split(n)
连续2天的结果是这样的:
[[2]]
# A tibble: 2 x 5
Year Month Day Rainfall n
<int> <int> <int> <dbl> <int>
1 1986 1 22 248 2
2 1986 1 23 119. 2
我只想计算第一次出现的次数(连续几天),所以对于上面的输出,应该只有 1 次。连续3天以上的也一样。
之后,我想绘制所有结果的直方图(计数与持续时间)。
我不确定如何在 R 中执行此操作。
如有任何帮助,我将不胜感激。
只保留第一天是按 grp
分组(同时按 Year
和 Month
分组)并选择 row_number()
等于的行的问题1
:
conseq_days <- jan %>%
group_by(Year,Month) %>%
mutate(extreme = Rainfall > quantile(Rainfall,0.95,na.rm=TRUE),
grp = data.table::rleid(extreme)) %>%
filter(extreme) %>%
add_count(grp) %>%
group_by(grp, .add = TRUE) %>%
filter(row_number() == 1) %>%
ungroup %>%
select(-extreme, -grp)
然后,使用 ggplot2 绘制直方图很简单:
ggplot(conseq_days, aes(n)) + geom_histogram()
我将 1
的 rle
值替换为其相应的长度,这给出了 n
连续降雨天数。
jan <- transform(jan, extreme=+(Rainfall > quantile(Rainfall, .95)))
r <- rle(jan$extreme)
r$values[r$values == 1] <- r$lengths[r$values == 1]
jan <- transform(jan, n=unlist(Map(rep, r$values, r$lengths)))
然后我做一些过滤得到jan.ex
,
jan.ex <- jan[jan$n > 0, -5]
jan.ex[!duplicated(jan.ex$n), ]
# Year Month Day Rainfall n
# 372 1980 1 7 111.3 1
# 743 1981 1 12 315.9 3
# 744 1981 1 13 246.3 3
# 745 1981 1 14 131.1 3
# 754 1981 1 23 122.2 1
# 762 1981 1 31 248.9 1
# 1106 1982 1 10 150.6 1
# 1125 1982 1 29 144.5 2
# 1126 1982 1 30 250.6 2
# 2193 1985 1 1 203.7 1
# 2196 1985 1 4 129.8 1
# 2575 1986 1 18 110.4 1
# 2579 1986 1 22 248.0 2
# 2580 1986 1 23 119.2 2
终于没有“重复”了。
res <- jan.ex[c(1, diff(jan.ex$n)) != 0, ]
res
# Year Month Day Rainfall n
# 372 1980 1 7 111.3 1
# 743 1981 1 12 315.9 3
# 754 1981 1 23 122.2 1
# 1125 1982 1 29 144.5 2
# 2193 1985 1 1 203.7 1
# 2579 1986 1 22 248.0 2
barplot
可能比直方图更适合。
barplot(table(res$n), col=2, main="Extreme rainfalls", xlab="Duration", ylab="Frequency")
我有以下数据:
dat <- structure(list(Year = c(1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1987L,
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L,
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L,
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L,
1987L, 1987L, 1987L), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
Day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L), Rainfall = c(1, 35.5, 20.3, 2.5,
32, 66.8, 0, 0, 1.8, 0, 5.3, 0, 0, 0, 11.7, 40.4, 45.7, 15.3,
21.6, 10.5, 26.2, 54.1, 1.5, 26.9, 39.4, 21.6, 1.3, 95.6,
10.2, 0, 5.1, 9.1, 31.5, 24.6, 18.5, 0, 37.1, 111.3, 4.3,
21.1, 3.1, 0, 0, 0, 15.8, 30.7, 6.4, 68.6, 97.5, 64.3, 47.3,
0, 2.3, 8.7, 53.9, 6.9, 20.9, 94, 7.4, 0, 1.3, 0, 38.6, 2.5,
10.4, 22.6, 6.3, 20.2, 3.6, 30.8, 0.8, 0.5, 34.3, 315.9,
246.3, 131.1, 11.8, 4.9, 2.1, 0.6, 0, 0, 0.5, 0.3, 122.2,
85.9, 40.2, 9.4, 15.4, 27.7, 2.3, 41.1, 248.9, 0.8, 0, 9.3,
10.9, 6.4, 0.3, 0.8, 0, 56.2, 150.6, 80, 12.2, 3.3, 0, 10.4,
95.3, 53, 55.9, 6, 46.2, 19.8, 1, 1.5, 10, 12, 47.8, 10.9,
80.5, 144.5, 250.6, 17.1, 1, 0, 15, 7.3, 69, 14.2, 6.6, 4.1,
0, 0.8, 2.5, 6.1, 44.5, 0, 23.1, 0.5, 2.5, 11.2, 5.4, 6.1,
0.5, 0, 0, 0, 0, 3.8, 0, 15.6, 16.3, 0.8, 1.3, 88.7, 18.3,
0.9, 1.3, 1.5, 0.5, 0.6, 5.3, 60.2, 0, 0.8, 19.1, 83.8, 62.7,
51.6, 3.3, 33.8, 7.6, 8.1, 0, 0, 16.3, 19, 28.2, 35.6, 2.9,
1.5, 17.8, 38.8, 15.8, 18.8, 203.7, 13.5, 30, 129.8, 5.6,
2, 0, 0, 0, 0, 0, 0, 0, 1.3, 0, 1, 2.5, 0, 3.8, 9.4, 1, 0,
24.6, 11.4, 11.7, 11.4, 0, 0, 0, 2.3, 8.6, 3, 14.4, 20.4,
4.2, 1.4, 20.8, 58.4, 42.9, 45.4, 67.6, 19.5, 4, 8, 24.9,
6.9, 75, 52.2, 110.4, 76.1, 10.6, 44.6, 248, 119.2, 1.2,
2, 9.5, 4.6, 29, 5.7, 49.6, 0, 0, 3, 22.4, 3, 3.2, 0, 26.2,
11, 8.8, 0, 4.2, 2, 9.4, 23.6, 76.8, 1, 60.6, 25.4, 6.2,
15.9, 9.2, 2, 0, 23.2, 22.8, 15, 4.2, 10.5, 2.8, 6, 26)), na.action = structure(c(`2000` =
2000L,
`2007` = 2007L, `3515` = 3515L, `3653` = 3653L, `10318` = 10318L,
`10319` = 10319L, `10320` = 10320L, `10321` = 10321L, `10322` = 10322L,
`10323` = 10323L, `10324` = 10324L, `10325` = 10325L, `10326` = 10326L,
`10327` = 10327L, `10328` = 10328L, `10329` = 10329L, `10330` = 10330L,
`10331` = 10331L, `10332` = 10332L, `10333` = 10333L, `10334` = 10334L,
`10335` = 10335L, `10336` = 10336L, `10337` = 10337L, `10338` = 10338L,
`10339` = 10339L, `10340` = 10340L, `10341` = 10341L, `10342` = 10342L,
`10343` = 10343L, `10344` = 10344L, `10345` = 10345L, `10346` = 10346L,
`10347` = 10347L, `10501` = 10501L, `10502` = 10502L, `10503` = 10503L,
`10504` = 10504L, `10505` = 10505L, `10506` = 10506L, `10507` = 10507L,
`10508` = 10508L, `10509` = 10509L, `10510` = 10510L, `10511` = 10511L,
`10512` = 10512L, `10513` = 10513L, `10514` = 10514L, `10515` = 10515L,
`10516` = 10516L, `10517` = 10517L, `10518` = 10518L, `10519` = 10519L,
`10520` = 10520L, `10521` = 10521L, `10522` = 10522L, `10523` = 10523L,
`10524` = 10524L, `10525` = 10525L, `10526` = 10526L, `10527` = 10527L,
`10528` = 10528L, `10529` = 10529L, `10530` = 10530L, `10531` = 10531L,
`10593` = 10593L, `10594` = 10594L, `10595` = 10595L, `10596` = 10596L,
`10597` = 10597L, `10598` = 10598L, `10599` = 10599L, `10600` = 10600L,
`10601` = 10601L, `10602` = 10602L, `10603` = 10603L, `10604` = 10604L,
`10605` = 10605L, `10606` = 10606L, `10607` = 10607L, `10608` = 10608L,
`10609` = 10609L, `10610` = 10610L, `10611` = 10611L, `10612` = 10612L,
`10613` = 10613L, `10614` = 10614L, `10615` = 10615L, `10616` = 10616L,
`10617` = 10617L, `10618` = 10618L, `10619` = 10619L, `10620` = 10620L,
`10621` = 10621L, `10622` = 10622L, `10623` = 10623L, `10624` = 10624L,
`10625` = 10625L, `10626` = 10626L, `10627` = 10627L, `10628` = 10628L,
`10629` = 10629L, `10630` = 10630L, `10631` = 10631L, `10632` = 10632L,
`10633` = 10633L, `10634` = 10634L, `10635` = 10635L, `10636` = 10636L,
`10637` = 10637L, `10638` = 10638L, `10639` = 10639L, `10640` = 10640L,
`10641` = 10641L, `10642` = 10642L, `10643` = 10643L, `10644` = 10644L,
`10645` = 10645L, `10646` = 10646L, `10647` = 10647L, `10648` = 10648L,
`10649` = 10649L, `10650` = 10650L, `10651` = 10651L, `10652` = 10652L,
`10653` = 10653L, `10654` = 10654L, `10655` = 10655L, `10656` = 10656L,
`10657` = 10657L, `10658` = 10658L, `10659` = 10659L, `10660` = 10660L,
`10661` = 10661L, `10662` = 10662L, `10663` = 10663L, `10664` = 10664L,
`10665` = 10665L, `10666` = 10666L, `10667` = 10667L, `10668` = 10668L,
`10669` = 10669L, `10670` = 10670L, `10671` = 10671L, `10672` = 10672L,
`10673` = 10673L, `10674` = 10674L, `10675` = 10675L, `10676` = 10676L,
`10677` = 10677L, `10678` = 10678L, `10679` = 10679L, `10680` = 10680L,
`10681` = 10681L, `10682` = 10682L, `10683` = 10683L, `10775` = 10775L,
`10776` = 10776L, `10777` = 10777L, `10778` = 10778L, `10779` = 10779L,
`10780` = 10780L, `10781` = 10781L, `10782` = 10782L, `10783` = 10783L,
`10784` = 10784L, `10785` = 10785L, `10786` = 10786L, `10787` = 10787L,
`10788` = 10788L, `10789` = 10789L, `10790` = 10790L, `10791` = 10791L,
`10792` = 10792L, `10793` = 10793L, `10794` = 10794L, `10795` = 10795L,
`10796` = 10796L, `10797` = 10797L, `10798` = 10798L, `10799` = 10799L,
`10800` = 10800L, `10801` = 10801L, `10802` = 10802L, `10803` = 10803L,
`10804` = 10804L, `10805` = 10805L, `10928` = 10928L, `10929` = 10929L,
`10930` = 10930L, `10931` = 10931L, `10932` = 10932L, `10933` = 10933L,
`10934` = 10934L, `10935` = 10935L, `10936` = 10936L, `10937` = 10937L,
`10938` = 10938L, `10939` = 10939L, `10940` = 10940L, `10941` = 10941L,
`10942` = 10942L, `10943` = 10943L, `10944` = 10944L, `10945` = 10945L,
`10946` = 10946L, `10947` = 10947L, `10948` = 10948L, `10949` = 10949L,
`10950` = 10950L, `10951` = 10951L, `10952` = 10952L, `10953` = 10953L,
`10954` = 10954L, `10955` = 10955L, `10956` = 10956L, `10957` = 10957L,
`10958` = 10958L, `11306` = 11306L, `11307` = 11307L, `11308` = 11308L,
`11309` = 11309L, `11310` = 11310L, `11311` = 11311L, `11312` = 11312L,
`11444` = 11444L, `11445` = 11445L, `11446` = 11446L, `11447` = 11447L,
`11448` = 11448L, `11449` = 11449L, `11450` = 11450L, `11451` = 11451L,
`11452` = 11452L, `11453` = 11453L, `11454` = 11454L, `11455` = 11455L,
`11456` = 11456L, `11457` = 11457L, `11458` = 11458L, `11459` = 11459L,
`11460` = 11460L, `11461` = 11461L, `11462` = 11462L, `11463` = 11463L,
`11464` = 11464L, `11465` = 11465L, `11466` = 11466L, `11467` = 11467L,
`11468` = 11468L, `11469` = 11469L, `11470` = 11470L, `11471` = 11471L,
`11472` = 11472L, `11473` = 11473L, `11474` = 11474L, `11658` = 11658L,
`11659` = 11659L, `11660` = 11660L, `11661` = 11661L, `11662` = 11662L,
`11663` = 11663L, `11664` = 11664L, `11665` = 11665L, `11666` = 11666L,
`11667` = 11667L, `11668` = 11668L, `11669` = 11669L, `11670` = 11670L,
`11671` = 11671L, `11672` = 11672L, `11673` = 11673L, `11674` = 11674L,
`11675` = 11675L, `11676` = 11676L, `11677` = 11677L, `11678` = 11678L,
`11679` = 11679L, `11680` = 11680L, `11681` = 11681L, `11682` = 11682L,
`11683` = 11683L, `11684` = 11684L, `11685` = 11685L, `11686` = 11686L,
`11687` = 11687L, `11688` = 11688L, `12389` = 12389L, `12390` = 12390L,
`12391` = 12391L, `12392` = 12392L, `12393` = 12393L, `12394` = 12394L,
`12395` = 12395L, `12396` = 12396L, `12397` = 12397L, `12398` = 12398L,
`12399` = 12399L, `12400` = 12400L, `12401` = 12401L, `12402` = 12402L,
`12403` = 12403L, `12404` = 12404L, `12405` = 12405L, `12406` = 12406L,
`12407` = 12407L, `12408` = 12408L, `12409` = 12409L, `12410` = 12410L,
`12411` = 12411L, `12412` = 12412L, `12413` = 12413L, `12414` = 12414L,
`12415` = 12415L, `12416` = 12416L, `12417` = 12417L, `12418` = 12418L,
`12419` = 12419L, `13270` = 13270L, `13271` = 13271L, `13272` = 13272L,
`13273` = 13273L, `13274` = 13274L, `13275` = 13275L, `13276` = 13276L,
`13277` = 13277L, `13278` = 13278L, `13279` = 13279L, `13280` = 13280L,
`13281` = 13281L, `13282` = 13282L, `13283` = 13283L, `13284` = 13284L,
`13285` = 13285L, `13286` = 13286L, `13287` = 13287L, `13288` = 13288L,
`13289` = 13289L, `13290` = 13290L, `13291` = 13291L, `13292` = 13292L,
`13293` = 13293L, `13294` = 13294L, `13295` = 13295L, `13296` = 13296L,
`13297` = 13297L, `13298` = 13298L, `13299` = 13299L, `13300` = 13300L,
`13362` = 13362L, `13363` = 13363L, `13364` = 13364L, `13365` = 13365L,
`13366` = 13366L, `13367` = 13367L, `13368` = 13368L, `13369` = 13369L,
`13370` = 13370L, `13371` = 13371L, `13372` = 13372L, `13373` = 13373L,
`13374` = 13374L, `13375` = 13375L, `13376` = 13376L, `13377` = 13377L,
`13378` = 13378L, `13379` = 13379L, `13380` = 13380L, `13381` = 13381L,
`13382` = 13382L, `13383` = 13383L, `13384` = 13384L, `13385` = 13385L,
`13386` = 13386L, `13387` = 13387L, `13388` = 13388L, `13389` = 13389L,
`13390` = 13390L, `13391` = 13391L, `13392` = 13392L, `14050` = 14050L
), class = "omit"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 366L,
367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L, 377L,
378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L, 388L,
389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L, 732L, 733L, 734L,
735L, 736L, 737L, 738L, 739L, 740L, 741L, 742L, 743L, 744L, 745L,
746L, 747L, 748L, 749L, 750L, 751L, 752L, 753L, 754L, 755L, 756L,
757L, 758L, 759L, 760L, 761L, 762L, 1097L, 1098L, 1099L, 1100L,
1101L, 1102L, 1103L, 1104L, 1105L, 1106L, 1107L, 1108L, 1109L,
1110L, 1111L, 1112L, 1113L, 1114L, 1115L, 1116L, 1117L, 1118L,
1119L, 1120L, 1121L, 1122L, 1123L, 1124L, 1125L, 1126L, 1127L,
1462L, 1463L, 1464L, 1465L, 1466L, 1467L, 1468L, 1469L, 1470L,
1471L, 1472L, 1473L, 1474L, 1475L, 1476L, 1477L, 1478L, 1479L,
1480L, 1481L, 1482L, 1483L, 1484L, 1485L, 1486L, 1487L, 1488L,
1489L, 1490L, 1491L, 1492L, 1827L, 1828L, 1829L, 1830L, 1831L,
1832L, 1833L, 1834L, 1835L, 1836L, 1837L, 1838L, 1839L, 1840L,
1841L, 1842L, 1843L, 1844L, 1845L, 1846L, 1847L, 1848L, 1849L,
1850L, 1851L, 1852L, 1853L, 1854L, 1855L, 1856L, 1857L, 2193L,
2194L, 2195L, 2196L, 2197L, 2198L, 2199L, 2200L, 2201L, 2202L,
2203L, 2204L, 2205L, 2206L, 2207L, 2208L, 2209L, 2210L, 2211L,
2212L, 2213L, 2214L, 2215L, 2216L, 2217L, 2218L, 2219L, 2220L,
2221L, 2222L, 2223L, 2558L, 2559L, 2560L, 2561L, 2562L, 2563L,
2564L, 2565L, 2566L, 2567L, 2568L, 2569L, 2570L, 2571L, 2572L,
2573L, 2574L, 2575L, 2576L, 2577L, 2578L, 2579L, 2580L, 2581L,
2582L, 2583L, 2584L, 2585L, 2586L, 2587L, 2588L, 2923L, 2924L,
2925L, 2926L, 2927L, 2928L, 2929L, 2930L, 2931L, 2932L, 2933L,
2934L, 2935L, 2936L, 2937L, 2938L, 2939L, 2940L, 2941L, 2942L,
2943L, 2944L, 2945L, 2946L, 2947L, 2948L, 2949L, 2950L, 2951L,
2952L, 2953L), class = "data.frame")
我正在计算超过第 95 个百分位数的天数,持续时间为 1 天、2 天等。
这是脚本:
jan %>%
group_by(Year,Month) %>%
mutate(extreme = Rainfall > quantile(Rainfall,0.95,na.rm=TRUE),
grp = data.table::rleid(extreme)) %>%
filter(extreme) %>%
add_count(grp) %>%
ungroup %>%
select(-extreme, -grp) %>%
group_split(n)
连续2天的结果是这样的:
[[2]]
# A tibble: 2 x 5
Year Month Day Rainfall n
<int> <int> <int> <dbl> <int>
1 1986 1 22 248 2
2 1986 1 23 119. 2
我只想计算第一次出现的次数(连续几天),所以对于上面的输出,应该只有 1 次。连续3天以上的也一样。
之后,我想绘制所有结果的直方图(计数与持续时间)。
我不确定如何在 R 中执行此操作。
如有任何帮助,我将不胜感激。
只保留第一天是按 grp
分组(同时按 Year
和 Month
分组)并选择 row_number()
等于的行的问题1
:
conseq_days <- jan %>%
group_by(Year,Month) %>%
mutate(extreme = Rainfall > quantile(Rainfall,0.95,na.rm=TRUE),
grp = data.table::rleid(extreme)) %>%
filter(extreme) %>%
add_count(grp) %>%
group_by(grp, .add = TRUE) %>%
filter(row_number() == 1) %>%
ungroup %>%
select(-extreme, -grp)
然后,使用 ggplot2 绘制直方图很简单:
ggplot(conseq_days, aes(n)) + geom_histogram()
我将 1
的 rle
值替换为其相应的长度,这给出了 n
连续降雨天数。
jan <- transform(jan, extreme=+(Rainfall > quantile(Rainfall, .95)))
r <- rle(jan$extreme)
r$values[r$values == 1] <- r$lengths[r$values == 1]
jan <- transform(jan, n=unlist(Map(rep, r$values, r$lengths)))
然后我做一些过滤得到jan.ex
,
jan.ex <- jan[jan$n > 0, -5]
jan.ex[!duplicated(jan.ex$n), ]
# Year Month Day Rainfall n
# 372 1980 1 7 111.3 1
# 743 1981 1 12 315.9 3
# 744 1981 1 13 246.3 3
# 745 1981 1 14 131.1 3
# 754 1981 1 23 122.2 1
# 762 1981 1 31 248.9 1
# 1106 1982 1 10 150.6 1
# 1125 1982 1 29 144.5 2
# 1126 1982 1 30 250.6 2
# 2193 1985 1 1 203.7 1
# 2196 1985 1 4 129.8 1
# 2575 1986 1 18 110.4 1
# 2579 1986 1 22 248.0 2
# 2580 1986 1 23 119.2 2
终于没有“重复”了。
res <- jan.ex[c(1, diff(jan.ex$n)) != 0, ]
res
# Year Month Day Rainfall n
# 372 1980 1 7 111.3 1
# 743 1981 1 12 315.9 3
# 754 1981 1 23 122.2 1
# 1125 1982 1 29 144.5 2
# 2193 1985 1 1 203.7 1
# 2579 1986 1 22 248.0 2
barplot
可能比直方图更适合。
barplot(table(res$n), col=2, main="Extreme rainfalls", xlab="Duration", ylab="Frequency")