我,我很难理解我原本以为是直截了当的东西。形成一个简洁的问题很棘手,这可能意味着我的逻辑有缺陷。
我想创建一个Sankey图,该图将收入流与两个交易列表中的借方借项相对应。我在创建加权邻接矩阵时遇到了麻烦,这可能是由于我坚持以dplyr方式进行处理。
将两个列表合并可以使我得到一个明智的优势列表(从收入到借方),但是然后按组计算权重(转移的金额)会限制我限制超支和使用任何剩余收入的能力。随后的支出。
理想情况下,最终结果将满足四个条件
我下面的方法不能满足条件2和4。在计算任何盈余时,收入5的支出超支了500,应由支出4的剩余收入来弥补
请告诉我我是否完全滑雪,这绝对是一个以前已经解决的问题。
library(tidyverse)
library(lubridate)
inc <- tribble(
~income, ~date, ~incoming_amount,
1L, "2015-01-01", 1000,
2L, "2016-01-01", 1000,
3L, "2017-01-01", 1000,
4L, "2018-01-01", 1000,
5L, "2019-01-01", 1000) %>%
mutate_at(vars(date), as_date)
deb <- tribble(
~debit, ~date, ~outgoing_amount,
1L, "2015-02-01", 200,
2L, "2015-10-01", 800,
3L, "2017-03-01", 1200,
4L, "2017-09-01", 800,
5L, "2018-04-01", 500,
6L, "2019-05-01", 750,
7L, "2019-06-01", 750) %>%
mutate_at(vars(date), as_date)
edge <- merge(inc, deb, all = T) %>%
select(-date) %>%
fill(income, incoming_amount, .direction = "down") %>%
fill(debit, outgoing_amount, .direction = "up") %>%
unique()
print(edge)
#> income incoming_amount debit outgoing_amount
#> 1 1 1000 1 200
#> 3 1 1000 2 800
#> 4 2 1000 3 1200
#> 5 3 1000 3 1200
#> 7 3 1000 4 800
#> 8 4 1000 5 500
#> 10 5 1000 6 750
#> 12 5 1000 7 750
weight <- group_by(edge, debit) %>%
mutate(available_amount = cumsum(lag(incoming_amount, default = 0)),
debit_balance = pmax(outgoing_amount - available_amount, 0),
amount_transferred = pmin(incoming_amount, debit_balance)) %>%
select(income, debit, amount_transferred)
print(weight)
#> # A tibble: 8 x 3
#> # Groups: debit [7]
#> income debit amount_transferred
#> <int> <int> <dbl>
#> 1 1 1 200
#> 2 1 2 800
#> 3 2 3 1000
#> 4 3 3 200
#> 5 3 4 800
#> 6 4 5 500
#> 7 5 6 750
#> 8 5 7 750
surplus = group_by(weight, income) %>%
summarise(total_transferred = sum(amount_transferred)) %>%
left_join(inc) %>%
mutate(surplus = incoming_amount - total_transferred)
#> Joining, by = "income"
print(surplus)
#> # A tibble: 5 x 5
#> income total_transferred date incoming_amount surplus
#> <int> <dbl> <date> <dbl> <dbl>
#> 1 1 1000 2015-01-01 1000 0
#> 2 2 1000 2016-01-01 1000 0
#> 3 3 1000 2017-01-01 1000 0
#> 4 4 500 2018-01-01 1000 500
#> 5 5 1500 2019-01-01 1000 -500
由reprex package(v0.3.0)于2019-07-17创建