如何解决使用R中的列计算两个日期之间的平均值时出现问题
我需要根据两个日期计算一列的平均值。数据表如下所示。
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之间的日期,我需要获取均值列的平均值。所需的输出如下所示。平均date列中日期的平均列值的平均值介于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解决方案。
谢谢
解决方法
不确定是否有多个组,是否需要计算每个组的均值。如果是这种情况,请查看以下代码是否对您有用:
> 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,acres,by = c('pol' = 'pol','id' = 'id','acres' = 'acres',na.rm = T)))
使用您的代码:
> df %>%
+ left_join(df %>% group_by(pol,acres) %>% filter(date> st_date & date < end_date) %>%
+ mutate(avg = mean(mean)) %>% select(pol,keep = F) %>%
+ mutate(avg = replace_na(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
>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。