What data structure to use for financial model

Hi all,

I am new to R and I am trying to put my financial model from excel to R (sounds weird but I actually got some progress. The question is - the structure of my model is like that:

Account_1 (i.e. Revenue)
2020 2021 2022 2023 ...
obj_1 ... ...
obj_2
obj_3
... ... ...

Account_2 (i.e. Prices)
2020 2021 2022 2023 ...
obj_1 ... ...
obj_2
obj_3
... ... ...

Account_3 (i.e. Volumes)
2020 2021 2022 2023 ...
obj_1 ... ...
obj_2
obj_3
... ... ...

Obviously, Revenue = Prices * Volumes. If there was only one object, then I would turn them into named vectors and do pretty simple vector arithmetics. I could also mutate them into one dataframe with Years as an index.
But with multiple objects, I don't know how to deal with such 2-dimensional array. The goal is to simplify overall arithmetics as possible, but also to mutate these 2-dimensional different accounts in some sort of df.
I need also to be able to refer (+filter, +subset) to either object or year of such array.

What data structure would you suggest me to use? Matrix or lists or what?

Thank you,

Image is always better
image

One data frame with columns for year, object name, volume, price, revenue.

It’s generally best to have

  • one large data frame instead of many smalls
  • columns that contain everything of same measure (one revenue column instead of multiple)
  • long format instead of wide (years in rows not columns)

This will allow you to most easily aggregate and plot.

‘df %>% group_by(product) %>% summarize(sum(revenue))’

‘df %>% ggplot() + aes(year, revenue, color = product) + geom_line()’

Thank you!

Looks like the best option and most corresponding to "tidy" approach :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.