<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Cash Flow on R Views</title>
    <link>https://rviews.rstudio.com/tags/cash-flow/</link>
    <description>Recent content in Cash Flow on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 05 Oct 2022 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/cash-flow/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Money, Money, Money: RConcillation</title>
      <link>https://rviews.rstudio.com/2022/10/05/cash-flow-rconciliation/</link>
      <pubDate>Wed, 05 Oct 2022 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2022/10/05/cash-flow-rconciliation/</guid>
      <description>
        


&lt;p&gt;&lt;em&gt;Dr. Maria Prokofieva is a member of the R / Business working group which is promoting the use of R in accounting, auditing, and actuarial work. She is also a professor at the Victoria University Business School in Australia and works with CPA Australia.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We continue with our series for “nerdy” accountants who want to diverge from Excel and master the power and beauty of R automation by looking at one of the most important areas of ANY business! Cash!&lt;/p&gt;
&lt;p&gt;Cash management is a really critical issue for both business owners and people like me who are trying not to look at recent interest rates jumps.&lt;/p&gt;
&lt;p&gt;Cash management includes cash collection, handling, and usage of cash (spending!). It is essential to have &lt;strong&gt;enough&lt;/strong&gt; cash to cover immediate expenses, fund business growth and have working capital. Or in simple terms, you need to have enough cash to pay for your coffee, cover your mortgage repayment and invest in that &lt;a href=&#34;https://www.tesla.com/en_au/model3&#34;&gt;Tesla Model 3&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;tesla.jpeg&#34; height = &#34;300&#34; width=&#34;400&#34; alt=&#34;Red Tesla Model 3&#34;&gt;&lt;/p&gt;
&lt;p&gt;Cash analysis is an important step to assess companies short-term liquidity, evaluate working capital and make decisions about investments.&lt;/p&gt;
&lt;p&gt;Today, we are going to have a look at the step that comes before cash flow visualization. Much much earlier…. Before we are able to put cash flow items on a nice graph, we need to obtain those cash flow items “somehow”.&lt;/p&gt;
&lt;p&gt;Accountants don’t have cash flow data by default, and there is no magic way to get it. Rather, it is necessary to go transaction by transaction, classify items, group them, collate them, and double-check that they actually occurred! We need to make sure that we are not double-charged as well as we are not underpaying or omitting any of our payments and they are all included in the list.&lt;/p&gt;
&lt;p&gt;We start backwards from this very list and we dig into doing bank reconciliation and in particular, looking at our (business) bank statement. This is indeed a very useful exercise, not only in regards to your business but also for your own expense management.&lt;/p&gt;
&lt;p&gt;For this post, we will work through a very simple example, just looking at a bank statement and poking around. It is a “personal” bank statement that comes from &lt;a href=&#34;https://www.kaggle.com/datasets/sandhaya4u/august-bank-statement-sandhaya&#34;&gt;Kaggle&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf&amp;lt;-read_csv(&amp;quot;bank_st.csv&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Rows: 107 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: &amp;quot;,&amp;quot;
## chr (4): Date, Day, Type, Category
## dbl (3): Debit Amount, Credit Amount, Closing Balance
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%head()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 6 × 7
##   Date     Day    Type  Category `Debit Amount` `Credit Amount` `Closing Balan…`
##   &amp;lt;chr&amp;gt;    &amp;lt;chr&amp;gt;  &amp;lt;chr&amp;gt; &amp;lt;chr&amp;gt;             &amp;lt;dbl&amp;gt;           &amp;lt;dbl&amp;gt;            &amp;lt;dbl&amp;gt;
## 1 1/8/2018 Wedne… Debit Shopping          2500                0          174656.
## 2 1/8/2018 Wedne… Debit Shopping           324                0          174332.
## 3 2/8/2018 Thurs… None  None                 0                0          174332.
## 4 3/8/2018 Friday Debit Shopping           404.               0          173928.
## 5 4/8/2018 Satur… Debit Shopping           100                0          173828.
## 6 4/8/2018 Satur… Debit Shopping          1395                0          172433.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a typical bank statement you can view in your bank account where each row is a transaction for a particular reporting period (e.g. month). We do not have the name of the second party for the transactions (e.g. the name of the store or the company that credited/debited the account), but all transactions have been classified - which can be seen under &lt;code&gt;Category&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The dataset has &lt;code&gt;Debit Amount&lt;/code&gt;, which is what you were charged, and &lt;code&gt;Credit Amount&lt;/code&gt;, which is what you were paid. The &lt;code&gt;Closing Balance&lt;/code&gt; is a running balance that shows the amount of cash in your account after the transaction. The most important parts of that &lt;code&gt;Closing Balance&lt;/code&gt; are the initial and final numbers and they are used to reconcile (= match) balances in your own “books” (accounting books!= accounting records). If those number do not match, we investigate individual closing balances for the transactions to identify where we were overpaid or underpaid.&lt;/p&gt;
&lt;p&gt;Let’s look closer at the data: it is not messy, but not ideal…&lt;/p&gt;
&lt;p&gt;Column names have blanks and they do not play well in functions, so let’s use &lt;code&gt;clean_names()&lt;/code&gt; from &lt;code&gt;janitor&lt;/code&gt; package to make them more R friendly&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf&amp;lt;-cf%&amp;gt;%
  clean_names()

cf%&amp;gt;%head()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 6 × 7
##   date     day       type  category debit_amount credit_amount closing_balance
##   &amp;lt;chr&amp;gt;    &amp;lt;chr&amp;gt;     &amp;lt;chr&amp;gt; &amp;lt;chr&amp;gt;           &amp;lt;dbl&amp;gt;         &amp;lt;dbl&amp;gt;           &amp;lt;dbl&amp;gt;
## 1 1/8/2018 Wednesday Debit Shopping        2500              0         174656.
## 2 1/8/2018 Wednesday Debit Shopping         324              0         174332.
## 3 2/8/2018 Thursday  None  None               0              0         174332.
## 4 3/8/2018 Friday    Debit Shopping         404.             0         173928.
## 5 4/8/2018 Saturday  Debit Shopping         100              0         173828.
## 6 4/8/2018 Saturday  Debit Shopping        1395              0         172433.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s better! so now all variables are in small letters and have snake_case!&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;names(cf)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;date&amp;quot;            &amp;quot;day&amp;quot;             &amp;quot;type&amp;quot;            &amp;quot;category&amp;quot;       
## [5] &amp;quot;debit_amount&amp;quot;    &amp;quot;credit_amount&amp;quot;   &amp;quot;closing_balance&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s explore the data and do some simple &lt;strong&gt;counting&lt;/strong&gt; - yes, we love to count!&lt;/p&gt;
&lt;p&gt;First, what is the closing balance and how does it change during the month? But before we do so, let’s have a close look at the &lt;code&gt;date&lt;/code&gt; column. In the first twenty rows you can see there are a few issues as some dates include single vs. double for days and two-digit vs. four-digit for year. It is also in a string format.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;class(cf$date)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;character&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf$date[1:20]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##  [1] &amp;quot;1/8/2018&amp;quot;  &amp;quot;1/8/2018&amp;quot;  &amp;quot;2/8/2018&amp;quot;  &amp;quot;3/8/2018&amp;quot;  &amp;quot;4/8/2018&amp;quot;  &amp;quot;4/8/2018&amp;quot; 
##  [7] &amp;quot;4/8/2018&amp;quot;  &amp;quot;4/8/2018&amp;quot;  &amp;quot;4/8/2018&amp;quot;  &amp;quot;5/8/2018&amp;quot;  &amp;quot;6/8/2018&amp;quot;  &amp;quot;6/8/2018&amp;quot; 
## [13] &amp;quot;7/8/2018&amp;quot;  &amp;quot;8/8/2018&amp;quot;  &amp;quot;9/8/2018&amp;quot;  &amp;quot;10/8/2018&amp;quot; &amp;quot;10/8/2018&amp;quot; &amp;quot;11/8/2018&amp;quot;
## [19] &amp;quot;11/8/2018&amp;quot; &amp;quot;11/8/2018&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To fix this, let’s convert to the date type and fix the formatting with &lt;code&gt;lubridate&lt;/code&gt; package.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf$date&amp;lt;-dmy(cf$date)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let’s see the spend per each billing date. We exclude the days with no spend:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%
  group_by(date)%&amp;gt;%
  summarise(spend=sum(debit_amount))%&amp;gt;%
  filter(spend!=0)%&amp;gt;%
  ggplot(aes(date, spend))+
  geom_line()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/2022/10/05/cash-flow-rconciliation/index_files/figure-html/unnamed-chunk-7-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Now, let’s see the type of categories we have.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%count(category, sort=TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 10 × 2
##    category          n
##    &amp;lt;chr&amp;gt;         &amp;lt;int&amp;gt;
##  1 Shopping         46
##  2 None             21
##  3 ATM               9
##  4 Interest          8
##  5 Entertainment     7
##  6 Medical           5
##  7 Travel            4
##  8 Restaurant        3
##  9 Rent              2
## 10 Salary            2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This &lt;code&gt;None&lt;/code&gt; category does not look right…. What is there?&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;% filter(category==&amp;quot;None&amp;quot;)%&amp;gt;%
  head()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 6 × 7
##   date       day       type  category debit_amount credit_amount closing_balance
##   &amp;lt;date&amp;gt;     &amp;lt;chr&amp;gt;     &amp;lt;chr&amp;gt; &amp;lt;chr&amp;gt;           &amp;lt;dbl&amp;gt;         &amp;lt;dbl&amp;gt;           &amp;lt;dbl&amp;gt;
## 1 2018-08-02 Thursday  None  None                0             0         174332.
## 2 2018-08-05 Sunday    None  None                0             0         162098.
## 3 2018-08-08 Wednesday None  None                0             0         158597.
## 4 2018-08-21 Tuesday   None  None                0             0          91343.
## 5 2018-08-24 Friday    None  None                0             0          61755.
## 6 2018-08-26 Sunday    None  None                0             0          38441.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It looks like the majority of these entries are not really transactions, but a closing balance. Do we need to include them? Probably not. Let’s confirm that they are not transactions and have &lt;code&gt;debit_amount&lt;/code&gt; and &lt;code&gt;credit_amount&lt;/code&gt; as zero&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;% filter(category==&amp;quot;None&amp;quot;)%&amp;gt;%
  filter(debit_amount!=0 | credit_amount!=0)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 0 × 7
## # … with 7 variables: date &amp;lt;date&amp;gt;, day &amp;lt;chr&amp;gt;, type &amp;lt;chr&amp;gt;, category &amp;lt;chr&amp;gt;,
## #   debit_amount &amp;lt;dbl&amp;gt;, credit_amount &amp;lt;dbl&amp;gt;, closing_balance &amp;lt;dbl&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and it is a good idea to exclude them&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf&amp;lt;-cf%&amp;gt;%filter(category!=&amp;quot;None&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s see which day has the most number of transactions and which category is the most used one (what is the money drainer!):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%count(day, sort=TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 7 × 2
##   day           n
##   &amp;lt;chr&amp;gt;     &amp;lt;int&amp;gt;
## 1 Saturday     36
## 2 Friday       11
## 3 Thursday     10
## 4 Sunday        9
## 5 Wednesday     8
## 6 Monday        7
## 7 Tuesday       5&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%count(category, sort=TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 9 × 2
##   category          n
##   &amp;lt;chr&amp;gt;         &amp;lt;int&amp;gt;
## 1 Shopping         46
## 2 ATM               9
## 3 Interest          8
## 4 Entertainment     7
## 5 Medical           5
## 6 Travel            4
## 7 Restaurant        3
## 8 Rent              2
## 9 Salary            2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Well, good, but does not look nice? So let’s “paint it”. (We look at spending where credited amount is $0 per category.)&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot4&amp;lt;-cf%&amp;gt;%filter(credit_amount==0)%&amp;gt;%
  group_by(day)%&amp;gt;%
  summarise(day_spend=sum(debit_amount),
            n=n())%&amp;gt;%
  ggplot(aes(x=fct_reorder(day, desc(day_spend)),
             y=day_spend))+
  geom_col()+ 
  labs(x = &amp;quot;Days&amp;quot;, y = &amp;quot;$ value&amp;quot;,
title =&amp;quot;Cash across days&amp;quot;)+
  theme(
  panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  axis.line = element_line(colour = &amp;quot;black&amp;quot;),
  axis.text.x = element_text(angle = 90),
plot.title = element_textbox(hjust = 0.5,
                                 width = unit(0.5, &amp;quot;npc&amp;quot;),
                                 margin = margin(b = 15))  )

(plot1|plot2)/(plot3|plot4)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/2022/10/05/cash-flow-rconciliation/index_files/figure-html/unnamed-chunk-14-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;For a real business, this amount of Saturday transactions would raise a red flag, but this data is from personal records, so it looks like someone is having a blast after a busy week :)&lt;/p&gt;
&lt;p&gt;Also, with &lt;code&gt;category&lt;/code&gt; that &lt;code&gt;None&lt;/code&gt; does not sound right…. it is the second highest so… I would really investigate what sort of &lt;code&gt;None&lt;/code&gt; is that &lt;code&gt;None&lt;/code&gt;…&lt;/p&gt;
&lt;p&gt;Well, what are out total earn and which days we are paid and what for?&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%filter(credit_amount&amp;gt;0)%&amp;gt;%
  count(category)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 2 × 2
##   category     n
##   &amp;lt;chr&amp;gt;    &amp;lt;int&amp;gt;
## 1 Interest     8
## 2 Salary       2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It looks like we have only two major category - interest and salary. Let’s see what brings more money&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cf%&amp;gt;%filter(credit_amount&amp;gt;0)%&amp;gt;%
  group_by(category)%&amp;gt;%
  summarise(category_total=sum(credit_amount))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 2 × 2
##   category category_total
##   &amp;lt;chr&amp;gt;             &amp;lt;dbl&amp;gt;
## 1 Interest          4050.
## 2 Salary          500508&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Well, it is still salary! but would be sooo good if it is our passive income that drives the cash flows!&lt;/p&gt;
&lt;p&gt;Let’s see the balance for the month…&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;balance&amp;lt;-sum(cf$credit_amount)-sum(cf$debit_amount)

balance&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 268715&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Woohoo! Our balance is positive, so we managed to grow our wealth!&lt;/p&gt;
&lt;p&gt;Indeed, it is a very simple example, but a good foundation to start your R experience in accounting!&lt;/p&gt;
&lt;p&gt;Done for today: we are ready for bank RConciliation. Stay tuned!
….&lt;/p&gt;
&lt;div id=&#34;references&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;References&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://www.kaggle.com/datasets/sandhaya4u/august-bank-statement-sandhaya&#34; class=&#34;uri&#34;&gt;https://www.kaggle.com/datasets/sandhaya4u/august-bank-statement-sandhaya&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2022/10/05/cash-flow-rconciliation/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>Looking at cash flows</title>
      <link>https://rviews.rstudio.com/2022/09/02/looking-at-cash-flows/</link>
      <pubDate>Fri, 02 Sep 2022 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2022/09/02/looking-at-cash-flows/</guid>
      <description>
        
&lt;script src=&#34;/2022/09/02/looking-at-cash-flows/index_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/2022/09/02/looking-at-cash-flows/index_files/d3/d3.min.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/2022/09/02/looking-at-cash-flows/index_files/sankey/sankey.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/2022/09/02/looking-at-cash-flows/index_files/sankeyNetwork-binding/sankeyNetwork.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;&lt;em&gt;Dr. Maria Prokofieva is a member of the R / Business working group which is promoting the use of R in accounting, auditing, and actuarial work. She is also a professor at the Victoria University Business School in Australia and works with CPA Australia.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let’s talk about cash flows!&lt;/p&gt;
&lt;p&gt;We continue our series of actuarial and accounting posts where we strive to interest the data science community in using their R skills for the actuarial and accounting applications. We also hope to help some actuaries and accountants with their R skills.&lt;/p&gt;
&lt;p&gt;Cash flow is a concept that is important to every business and household. It is difficult to over estimate the importance of disclosing and properly communicating the cash flow as an essential of running any enterprise. This is especially true in times of recession when cash flow is the elephant in the room. Nobody really wants to talk about it, but making cash flow analysis easier to understand and interpret would help almost everyone. To get the maximum impact from a cash flow analysis it is not only important to demonstrate that it has been truly and fairly reported (in accounting terminology), but also to help consumers of cash flow reports intuitively grasp the the meaning of what the numbers are showing.&lt;/p&gt;
&lt;p&gt;So what is the problem with the way we normally communicate information about cash flow? Well, look at the image below and check your pulse. How excited did you get? Can you make any sense of it at all with just a flash look?&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;cashflows.png&#34; alt=&#34;Cash flow table&#34; height=&#34;500&#34; width=&#34;600&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Actually, it’s incredibly insightful if you know what to look for, but for an non-accounting person (with powers to make decisions), it may not be.&lt;/p&gt;
&lt;p&gt;Now look at this example&lt;span class=&#34;math inline&#34;&gt;\(^1\)&lt;/span&gt; of a personal cash flow!&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://ppcexpo.com/blog/wp-content/uploads/2022/05/cash-flow-chart-16.jpg&#34; alt=&#34;Sankey chart from the ppcexpo.com blog&#34; height=&#34;300&#34; width=&#34;500&#34;/&gt;&lt;/p&gt;
&lt;p&gt;These numbers start talking to me in one insightful flow showing in a second where all the money goes! We all know that a picture is worth a thousand words, but if its millions of dollars, it’s truly priceless.&lt;/p&gt;
&lt;p&gt;This type of chart is called a &lt;a href=&#34;https://www.ifu.com/e-sankey/sankey-diagram/&#34;&gt;&lt;strong&gt;sankey&lt;/strong&gt;&lt;/a&gt; chart. Sankey charts are used to show material, energy and cost flows. You can view some awesome visualizations made with the &lt;code&gt;networkD3&lt;/code&gt; package &lt;a href=&#34;https://r-graph-gallery.com/sankey-diagram.html&#34;&gt;here&lt;/a&gt;. So, showing cash flow is a perfect example of what they were designed for. It is not all common in the actuarial or accounting worlds, but, honestly, it is not that common to see any visualizations in financial reports, - well, apart from happy customers in annual reports which, as we all know, is a marketing thing.&lt;/p&gt;
&lt;p&gt;Let’s take a look at how the flow diagram above might be reproduced in R. There are several ways to do this and several packages (for example: &lt;code&gt;networkD3&lt;/code&gt;, and &lt;code&gt;diagrammerR&lt;/code&gt;) available to help. We will use a very simple dataset, but it could be easily extended to include additional transactional data: different sources of income, types of income, etc. We could also look at inflows and outflows, classify them into types of business activities and automate this tedious, manual task to present a nice chart to our stakeholders.&lt;/p&gt;
&lt;p&gt;But. let’s start with … this simple one! And let’s use the &lt;a href=&#34;https://cran.r-project.org/web/packages/networkD3&#34;&gt;&lt;code&gt;networkD3&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;tidyverse.quiet = TRUE
networkD3.quiet = TRUE
library(tidyverse)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(networkD3)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sankey diagrams visualize &lt;strong&gt;flows&lt;/strong&gt; as connections from one &lt;strong&gt;node&lt;/strong&gt; to another. In our case, the flows are distinct types of cash income that will flow into expenses.&lt;/p&gt;
&lt;p&gt;For our example, we will use a simple data set with data that has already been classified into &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;data &amp;lt;- read_csv(&amp;quot;cf_up.csv&amp;quot;, show_col_types = FALSE)
head(data)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 6 × 3
##   source target        value
##   &amp;lt;chr&amp;gt;  &amp;lt;chr&amp;gt;         &amp;lt;dbl&amp;gt;
## 1 Salary Earned Income   494
## 2 Salary Earned Income   677
## 3 Salary Earned Income   758
## 4 Salary Earned Income   933
## 5 Salary Earned Income   649
## 6 Salary Earned Income   825&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;sources&lt;/em&gt; are nodes that initiate the flow and &lt;em&gt;targets&lt;/em&gt; are the nodes that finish the flow.&lt;/p&gt;
&lt;p&gt;In this particular example, the amount of cash receipts have been matched with a particular cash spending type. In real life, you would not expect this data matching to have already been done for you as most likely your cash flow records just show type of cash flow (cash receipt vs. cash payment) and the amount. We will save the data munging required to transform a typical cash flow dataset into something that can be used to build the Sankey diagram for a future post.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;data&lt;/code&gt; is a &lt;em&gt;links&lt;/em&gt; dataframe with source, target and value columns. For example, $494 of salary was used to pay tax: salary is the earned income and payment of tax is a deduction. In this case the source is the &lt;em&gt;Salary&lt;/em&gt; item, the target is &lt;em&gt;Earned income&lt;/em&gt; and the value is 494. &lt;em&gt;Earned income&lt;/em&gt;is also a source node that has &lt;em&gt;Income&lt;/em&gt; (i.e. a broader category) target. &lt;em&gt;Income&lt;/em&gt; is then distributed to expenses, i.e target category &lt;em&gt;Deduction&lt;/em&gt; which is the later becomes a source node for &lt;em&gt;Income tax&lt;/em&gt; target.&lt;/p&gt;
&lt;p&gt;Here are the unique sources in our data:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;unique(data$source)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##  [1] &amp;quot;Salary&amp;quot;                 &amp;quot;Credit Card Reward&amp;quot;     &amp;quot;Dividends&amp;quot;             
##  [4] &amp;quot;Interest&amp;quot;               &amp;quot;Earned Income&amp;quot;          &amp;quot;Passive Income&amp;quot;        
##  [7] &amp;quot;Passive income&amp;quot;         &amp;quot;Income&amp;quot;                 &amp;quot;Deduction&amp;quot;             
## [10] &amp;quot;Core Expenses&amp;quot;          &amp;quot;Financial Independence&amp;quot; &amp;quot;Disposable Income&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And, here are the unique targets.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;unique(data$target)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##  [1] &amp;quot;Earned Income&amp;quot;          &amp;quot;Passive Income&amp;quot;         &amp;quot;Passive income&amp;quot;        
##  [4] &amp;quot;Income&amp;quot;                 &amp;quot;Deduction&amp;quot;              &amp;quot;Core Expenses&amp;quot;         
##  [7] &amp;quot;Financial Independence&amp;quot; &amp;quot;Disposable Income&amp;quot;      &amp;quot;Income Tax&amp;quot;            
## [10] &amp;quot;Social Justice&amp;quot;         &amp;quot;Bill Expenses&amp;quot;          &amp;quot;Food&amp;quot;                  
## [13] &amp;quot;Personal Care&amp;quot;          &amp;quot;Transportation&amp;quot;         &amp;quot;Pension&amp;quot;               
## [16] &amp;quot;Investment&amp;quot;             &amp;quot;Real Estate&amp;quot;            &amp;quot;Emergency Fund&amp;quot;        
## [19] &amp;quot;Leisure&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To create a cash flow diagram with the &lt;code&gt;sankeyNetwork()&lt;/code&gt; function from the &lt;code&gt;networkD3&lt;/code&gt; package we also need an additional data frame, &lt;code&gt;nodes&lt;/code&gt; which contains the list of receipts, types of receipts, types of spending and cash payments.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;nodes &amp;lt;- data.frame(
  name=c(as.character(data$source), 
  as.character(data$target)) %&amp;gt;% unique()
)
head(nodes)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                 name
## 1             Salary
## 2 Credit Card Reward
## 3          Dividends
## 4           Interest
## 5      Earned Income
## 6     Passive Income&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also, because we cannot use nodes names in the function, we need to replace our connections with id, i.e. &lt;code&gt;IDsource&lt;/code&gt; and &lt;code&gt;IDtarget&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;data$IDsource &amp;lt;- match(data$source, nodes$name)-1 
data$IDtarget &amp;lt;- match(data$target, nodes$name)-1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we can draw our cash flow diagram.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p &amp;lt;- sankeyNetwork(Links = data, 
                   Nodes = nodes,
                   Source = &amp;quot;IDsource&amp;quot;, 
                   Target = &amp;quot;IDtarget&amp;quot;,
                   Value = &amp;quot;value&amp;quot;, 
                   NodeID = &amp;quot;name&amp;quot;,
                   units = &amp;#39;$&amp;#39;,
                   sinksRight=TRUE,
  #                 LinkGroup=&amp;quot;source&amp;quot;
                   fontSize = 10, 
                   nodeWidth = 30
                   
                   )&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Links is a tbl_df. Converting to a plain data frame.&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;htmlwidget-1&#34; style=&#34;width:672px;height:480px;&#34; class=&#34;sankeyNetwork html-widget&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-1&#34;&gt;{&#34;x&#34;:{&#34;links&#34;:{&#34;source&#34;:[0,0,0,0,0,0,0,0,1,2,3,4,4,4,4,4,4,4,4,5,6,6,7,7,7,7,7,7,7,7,7,7,7,8,8,9,9,9,9,10,10,10,11,11],&#34;target&#34;:[4,4,4,4,4,4,4,4,5,6,6,7,7,7,7,7,7,7,7,7,7,7,8,8,9,9,9,9,10,10,10,11,11,12,13,14,15,16,17,18,19,20,21,22],&#34;value&#34;:[494,677,758,933,649,825,536,392,287,262,147,494,677,758,933,649,825,536,392,287,262,147,494,677,758,933,649,825,536,392,287,262,147,494,677,758,933,649,825,536,392,287,262,147]},&#34;nodes&#34;:{&#34;name&#34;:[&#34;Salary&#34;,&#34;Credit Card Reward&#34;,&#34;Dividends&#34;,&#34;Interest&#34;,&#34;Earned Income&#34;,&#34;Passive Income&#34;,&#34;Passive income&#34;,&#34;Income&#34;,&#34;Deduction&#34;,&#34;Core Expenses&#34;,&#34;Financial Independence&#34;,&#34;Disposable Income&#34;,&#34;Income Tax&#34;,&#34;Social Justice&#34;,&#34;Bill Expenses&#34;,&#34;Food&#34;,&#34;Personal Care&#34;,&#34;Transportation&#34;,&#34;Pension&#34;,&#34;Investment&#34;,&#34;Real Estate&#34;,&#34;Emergency Fund&#34;,&#34;Leisure&#34;],&#34;group&#34;:[&#34;Salary&#34;,&#34;Credit Card Reward&#34;,&#34;Dividends&#34;,&#34;Interest&#34;,&#34;Earned Income&#34;,&#34;Passive Income&#34;,&#34;Passive income&#34;,&#34;Income&#34;,&#34;Deduction&#34;,&#34;Core Expenses&#34;,&#34;Financial Independence&#34;,&#34;Disposable Income&#34;,&#34;Income Tax&#34;,&#34;Social Justice&#34;,&#34;Bill Expenses&#34;,&#34;Food&#34;,&#34;Personal Care&#34;,&#34;Transportation&#34;,&#34;Pension&#34;,&#34;Investment&#34;,&#34;Real Estate&#34;,&#34;Emergency Fund&#34;,&#34;Leisure&#34;]},&#34;options&#34;:{&#34;NodeID&#34;:&#34;name&#34;,&#34;NodeGroup&#34;:&#34;name&#34;,&#34;LinkGroup&#34;:null,&#34;colourScale&#34;:&#34;d3.scaleOrdinal(d3.schemeCategory20);&#34;,&#34;fontSize&#34;:10,&#34;fontFamily&#34;:null,&#34;nodeWidth&#34;:30,&#34;nodePadding&#34;:10,&#34;units&#34;:&#34;$&#34;,&#34;margin&#34;:{&#34;top&#34;:null,&#34;right&#34;:null,&#34;bottom&#34;:null,&#34;left&#34;:null},&#34;iterations&#34;:32,&#34;sinksRight&#34;:true}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;There it is! In just a few lines of code you can turn a table that only an expert can read into a visualization that might capture the imagination of someone forced to read a financial document.&lt;/p&gt;
&lt;div id=&#34;references&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;PPCexpo. How to Create a Cash Flow Chart? Easy to Follow Steps. Available at &lt;a href=&#34;https://ppcexpo.com/blog/cash-flow-chart&#34; class=&#34;uri&#34;&gt;https://ppcexpo.com/blog/cash-flow-chart&lt;/a&gt; , Aug 31, 2022&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2022/09/02/looking-at-cash-flows/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
