考虑 R 中的输入和输出计算存储值
Calculate storage value considering inputs and outputs in R
我在执行水库平衡过程时遇到困难,我解释一下。
我想计算月末存储的值,考虑输入和输出(消耗)。
例子:
年初(一月初)水库是空的,所以有一个X值的输入,一个Y值的输出。剩下的将是储值。所以在 2 月份有一个新的输入(X 可能与 1 月份的 X 不同)并且退出保持不变。储值现在将为二月输入 + 一月储值 - 输出。依此类推,直到 12 月份。
条件是,如果结果为负,则考虑 0,如果大于 16000,则考虑 16000
我的数据是:
月
输入
退出
一月
4700
2250
fev
6990
2250
3月
8900
2250
abr
9000
2250
可能
5250
2250
君
2790
2250
七月
1770
2250
以前
492
2250
设置
89
2250
出来
572
2250
11 月
830
2250
dez
744
2250
预期结果:
月
输入
退出
已存储
一月
4700
2250
2516
fev
6990
2250
7256
3月
8900
2250
13980
abr
9000
2250
16000
可能
5250
2250
16000
君
2790
2250
15688
七月
1770
2250
14677
以前
492
2250
12772
设置
89
2250
10584
出来
572
2250
8735
11 月
830
2250
7066
dez
744
2250
5915
我在 excel 中设法做到了,但我需要自动化一些事情,这就是我使用 R
的原因
使用 purrr::accumulate
的 tidyverse 选项可能如下所示。
library(dplyr)
library(purrr)
bound_sum <- function(x, y) {
if (x+y > 16000)
16000
else if (x+y < 0)
0
else
x+y
}
df %>%
mutate(diff = case_when(row_number() == 1 & input-exit < 0 ~ 0,
row_number() == 1 & input-exit > 16000 ~ 16000,
T ~ input-exit),
stored = accumulate(diff, bound_sum)) %>%
select(-diff)
# # A tibble: 12 x 4
# month input exit stored
# <chr> <dbl> <dbl> <dbl>
# 1 jan 4700 2250 2450
# 2 fev 6990 2250 7190
# 3 mar 8900 2250 13840
# 4 abr 9000 2250 16000
# 5 may 5250 2250 16000
# 6 jun 2790 2250 16000
# 7 jul 1770 2250 15520
# 8 ago 492 2250 13762
# 9 set 89 2250 11601
# 10 out 572 2250 9923
# 11 nov 830 2250 8503
# 12 dez 744 2250 6997
数据
df <- structure(list(month = c("jan", "fev", "mar", "abr", "may", "jun",
"jul", "ago", "set", "out", "nov", "dez"), input = c(4700, 6990,
8900, 9000, 5250, 2790, 1770, 492, 89, 572, 830, 744), exit = c(2250,
2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250
)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))
我在执行水库平衡过程时遇到困难,我解释一下。
我想计算月末存储的值,考虑输入和输出(消耗)。
例子:
年初(一月初)水库是空的,所以有一个X值的输入,一个Y值的输出。剩下的将是储值。所以在 2 月份有一个新的输入(X 可能与 1 月份的 X 不同)并且退出保持不变。储值现在将为二月输入 + 一月储值 - 输出。依此类推,直到 12 月份。
条件是,如果结果为负,则考虑 0,如果大于 16000,则考虑 16000
我的数据是:
月 | 输入 | 退出 |
---|---|---|
一月 | 4700 | 2250 |
fev | 6990 | 2250 |
3月 | 8900 | 2250 |
abr | 9000 | 2250 |
可能 | 5250 | 2250 |
君 | 2790 | 2250 |
七月 | 1770 | 2250 |
以前 | 492 | 2250 |
设置 | 89 | 2250 |
出来 | 572 | 2250 |
11 月 | 830 | 2250 |
dez | 744 | 2250 |
预期结果:
月 | 输入 | 退出 | 已存储 |
---|---|---|---|
一月 | 4700 | 2250 | 2516 |
fev | 6990 | 2250 | 7256 |
3月 | 8900 | 2250 | 13980 |
abr | 9000 | 2250 | 16000 |
可能 | 5250 | 2250 | 16000 |
君 | 2790 | 2250 | 15688 |
七月 | 1770 | 2250 | 14677 |
以前 | 492 | 2250 | 12772 |
设置 | 89 | 2250 | 10584 |
出来 | 572 | 2250 | 8735 |
11 月 | 830 | 2250 | 7066 |
dez | 744 | 2250 | 5915 |
我在 excel 中设法做到了,但我需要自动化一些事情,这就是我使用 R
的原因使用 purrr::accumulate
的 tidyverse 选项可能如下所示。
library(dplyr)
library(purrr)
bound_sum <- function(x, y) {
if (x+y > 16000)
16000
else if (x+y < 0)
0
else
x+y
}
df %>%
mutate(diff = case_when(row_number() == 1 & input-exit < 0 ~ 0,
row_number() == 1 & input-exit > 16000 ~ 16000,
T ~ input-exit),
stored = accumulate(diff, bound_sum)) %>%
select(-diff)
# # A tibble: 12 x 4
# month input exit stored
# <chr> <dbl> <dbl> <dbl>
# 1 jan 4700 2250 2450
# 2 fev 6990 2250 7190
# 3 mar 8900 2250 13840
# 4 abr 9000 2250 16000
# 5 may 5250 2250 16000
# 6 jun 2790 2250 16000
# 7 jul 1770 2250 15520
# 8 ago 492 2250 13762
# 9 set 89 2250 11601
# 10 out 572 2250 9923
# 11 nov 830 2250 8503
# 12 dez 744 2250 6997
数据
df <- structure(list(month = c("jan", "fev", "mar", "abr", "may", "jun",
"jul", "ago", "set", "out", "nov", "dez"), input = c(4700, 6990,
8900, 9000, 5250, 2790, 1770, 492, 89, 572, 830, 744), exit = c(2250,
2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250, 2250
)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))