【问题标题】:How to find maximum value with date in each month in R?如何在R中找到每个月日期的最大值?
【发布时间】:2020-02-07 03:03:12
【问题描述】:

我正在寻找包含两个部分的问题的答案。在 A 部分:我想构建一个数据框,该数据框包含每个月的 total_precip(我的数据框中的变量)的最大值,其中包含其发生的年、月和日。在 B 部分中:我希望有另一个数据框,在其中我可以在每个月中连续两天获得最大累积 total_precip(即连续两天的总和高于任何其他两天),其中包含日期和相​​应的值。例如,如果 1 月 10 日和 11 日的 total_precip 总和高于该月的任何其他连续两天,则日期(年、月和相应的天数)及其值将存储在每个月的数据框中年。

这是我开始执行 A 部分的代码,但这仅给了我每个月的最大值,而没有指定最大值出现的日期。

library(weathercan)
library(tidyverse)
DF = weather_dl(station_ids = 2925, start = "1990-01-01", end = "1995-12-31", interval = "day")[,c(11,12,13,14,32)]
DF$month = as.numeric(DF$month)
DF$day = as.numeric(DF$day)
MaxValWithDate = DF %>% group_by(year, month) %>% summarise(MaxVal = max(total_precip))

【问题讨论】:

    标签: r dataframe dplyr sum max


    【解决方案1】:

    我们可以在 A 部分使用slice

    DF %>%
        group_by(year, month) %>%
        slice(which.max(total_precip))
    
     #   date       year  month   day total_precip
     #   <date>     <chr> <dbl> <dbl>        <dbl>
     # 1 1990-01-28 1990      1    28          7.8
     # 2 1990-02-21 1990      2    21          4.8
     # 3 1990-03-12 1990      3    12         49.2
     # ....
    

    然后我们可以在 B 部分再次使用 lead 函数和 slice

    DF %>%
        group_by(year, month) %>%
        mutate(lead_total_precip = lead(total_precip),
               lead_day = lead(date)) %>%
        mutate(cumu_precip = total_precip + lead_total_precip)  %>%
        slice(which.max(cumu_precip))
    
       # date       year  month   day total_precip lead_total_precip lead_day   cumu_precip
       # <date>     <chr> <dbl> <dbl>        <dbl>             <dbl> <date>           <dbl>
       # 1 1990-01-28 1990      1    28          7.8               5.2 1990-01-29        13  
       # 2 1990-02-21 1990      2    21          4.8               1.8 1990-02-22         6.6
       # 3 1990-03-11 1990      3    11          0                49.2 1990-03-12        49.2
       # ....
    

    生成的 data.frames 应该包含您需要的所有信息,然后您可以使用 select 函数仅保留您需要的列。

    【讨论】:

    • 谢谢。第一部分就像一个魅力。第二部分很好,但是,我想在数据框中包含两天的日期,当添加时会产生最大降水量。现在,如果您查看日期和月份列 - 它显示单个值(尽管提前一天的值已添加并显示在lead_day 列中。我想在不添加 total_precipation 的情况下获得这两天的日期,而是拥有日期连续两天降水量最大的total_precip。这有意义吗?谢谢
    • 不清楚您要的是什么。也许您应该在问题中发布您想要的输出示例。
    • 想要的输出应该是DesiredOutput = data.frame(date = c("1990-01-28","1990-01-29","1990-02-21","1990-02-22","1990-03-11","1990-03-12"), year = c(1990,1990,1990,1990,1990,1990), month = c(1,1,2,2,3,3), day = c(28,29,21,22,11,12), total_precip = c(7.8,5.2,4.8,1.8,0,49.2))
    • 另外,如果我想连续 5 天而不是两天收集累计 total_precipitation 的日期和值怎么办?我尝试修改之前的回复,如 DF %&gt;% group_by(year, month) %&gt;% mutate(lead_total_precip = lead(total_precip,4), lead_day = lead(date,4)) %&gt;% mutate(cumu_precip = total_precip + lead_total_precip) %&gt;% slice(which.max(cumu_precip)),但它会将第五天添加到计算中,而不是添加所有五天的 total_precip。
    • zoo 包中的函数可能会对您有所帮助(即rollsum
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多