【问题标题】:Issue in calculating mean between two dates using a column in R使用 R 中的列计算两个日期之间的平均值的问题
【发布时间】:2020-10-19 05:27:52
【问题描述】:

我需要根据两个日期计算列的平均值。数据表如下所示。

pol      id   acres    date           mean       st_date        end_date
12345    5    123.8    05_26_2019     0.2225     2019-07-24     2019-09-07
12345    5    123.8    06_11_2019     0.6523     2019-07-24     2019-09-07     
12345    5    123.8    06_27_2019     0.8563     2019-07-24     2019-09-07
12345    5    123.8    07_13_2019     0.1542     2019-07-24     2019-09-07
12345    5    123.8    07_29_2019     0.4253     2019-07-24     2019-09-07
12345    5    123.8    09_15_2019     0.1521     2019-07-24     2019-09-07
67890    4    60.0     05_05_2019     0.3652     2019-07-15     2019-08-31
67890    4    60.0     06_02_2019     0.4585     2019-07-15     2019-08-31
67890    4    60.0     07_10_2019     0.5856     2019-07-15     2019-08-31
67890    4    60.0     07_18_2019     0.6585     2019-07-15     2019-08-31
67890    4    60.0     09_02_2019     0.8585     2019-07-15     2019-08-31

我需要获取日期列中位于 st_date 和 end_date 之间的日期的平均列的平均值。所需的输出如下所示。平均column 日期列中日期的平均列值的平均值介于 st_date 和 end_date 之间。 (0.4253 + 0.1521)/2 = 0.2887

Output:

    pol      id   acres    date           mean       st_date        end_date       avg.
    12345    5    123.8    05_26_2019     0.2225     2019-07-24     2019-09-16     0.2887
    12345    5    123.8    06_11_2019     0.6523     2019-07-24     2019-09-16     0.2887
    12345    5    123.8    06_27_2019     0.8563     2019-07-24     2019-09-16     0.2887
    12345    5    123.8    07_13_2019     0.1542     2019-07-24     2019-09-16     0.2887
    12345    5    123.8    07_29_2019     0.4253     2019-07-24     2019-09-16     0.2887
    12345    5    123.8    09_15_2019     0.1521     2019-07-24     2019-09-16     0.2887

有人可以帮我解决这个问题吗?我更喜欢 data.table 解决方案。

谢谢,

【问题讨论】:

    标签: r date range mean


    【解决方案1】:

    不确定您是否有多个组,您需要计算每个组的平均值。如果是这种情况,请查看以下代码是否适合您:

    > library(dplyr)
    > library(tidyr)
    > df %>% 
    + left_join(df %>% group_by(id) %>% filter(date> st_date & date < end_date) %>% mutate(avg = mean(mean)) %>% select(id, date, avg), by = c('id' = 'id', 'date' = 'date'), keep = F) %>% mutate(avg = replace_na(avg, mean(avg, na.rm = T)))
    # A tibble: 6 x 8
        pol    id acres date        mean st_date    end_date     avg
      <dbl> <dbl> <dbl> <date>     <dbl> <date>     <date>     <dbl>
    1 12345     5  124. 2019-05-26 0.222 2019-07-24 2019-09-16 0.289
    2 12345     5  124. 2019-06-11 0.652 2019-07-24 2019-09-16 0.289
    3 12345     5  124. 2019-06-27 0.856 2019-07-24 2019-09-16 0.289
    4 12345     5  124. 2019-07-13 0.154 2019-07-24 2019-09-16 0.289
    5 12345     5  124. 2019-07-29 0.425 2019-07-24 2019-09-16 0.289
    6 12345     5  124. 2019-09-15 0.152 2019-07-24 2019-09-16 0.289
    > 
    

    我的代码:

    final_pl_date_sel %>% 
      left_join(df %>% group_by(pol,id,acres) %>% filter(date> st_date & date < end_date) %>% mutate(avg = mean(mean)) %>% select(pol, id, acres, date, avg), by = c('pol' = 'pol','id' = 'id','acres' = 'acres', 'date' = 'date'), keep = F) %>% mutate(avg = replace_na(avg, mean(avg, na.rm = T)))
    

    使用您的代码:

    > df %>% 
    +   left_join(df %>% group_by(pol, id, acres) %>% filter(date> st_date & date < end_date) %>% 
    +       mutate(avg = mean(mean)) %>% select(pol, id, acres, date, avg), by = c('pol' = 'pol','id' = 'id','acres' = 'acres', 'date' = 'date'), keep = F) %>% 
    +             mutate(avg = replace_na(avg, mean(avg, na.rm = T)))
    # A tibble: 6 x 8
        pol    id acres date        mean st_date    end_date     avg
      <dbl> <dbl> <dbl> <date>     <dbl> <date>     <date>     <dbl>
    1 12345     5  124. 2019-05-26 0.222 2019-07-24 2019-09-16 0.289
    2 12345     5  124. 2019-06-11 0.652 2019-07-24 2019-09-16 0.289
    3 12345     5  124. 2019-06-27 0.856 2019-07-24 2019-09-16 0.289
    4 12345     5  124. 2019-07-13 0.154 2019-07-24 2019-09-16 0.289
    5 12345     5  124. 2019-07-29 0.425 2019-07-24 2019-09-16 0.289
    6 12345     5  124. 2019-09-15 0.152 2019-07-24 2019-09-16 0.289
    

    使用“df”作为左表,因为我没有“final_pl_date_sel”表。

    我的 df:

    > df
    # A tibble: 6 x 7
        pol    id acres date        mean st_date    end_date  
      <dbl> <dbl> <dbl> <date>     <dbl> <date>     <date>    
    1 12345     5  124. 2019-05-26 0.222 2019-07-24 2019-09-16
    2 12345     5  124. 2019-06-11 0.652 2019-07-24 2019-09-16
    3 12345     5  124. 2019-06-27 0.856 2019-07-24 2019-09-16
    4 12345     5  124. 2019-07-13 0.154 2019-07-24 2019-09-16
    5 12345     5  124. 2019-07-29 0.425 2019-07-24 2019-09-16
    6 12345     5  124. 2019-09-15 0.152 2019-07-24 2019-09-16
    > 
    

    【讨论】:

    • 这就是我想要的。是的,我有多个组。你知道这个 data.table 的解决方案吗?谢谢。
    • 抱歉,我没有 data.table 解决方案,主要使用 tibble 和 tidyverse 包。因为他们也使用 data.tables
    • 当我在上面尝试时,我收到此错误“tbl_vars(y) 中的错误:缺少参数“y”,没有默认值”。不知道为什么。
    • 您能否逐字分享您正在执行的内容。
    • 实际上,当我尝试时,我得到了不同的答案。我需要按 pol、id 和英亩分组。我该如何纠正?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    相关资源
    最近更新 更多