This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

Working with Tidy Financial Data in tidyr – Part II

Robot Wealth

Contributor:
Robot Wealth
Visit: Robot Wealth

See Part I to get started with the necessary data and R packages.

The practical reality

A good rule of thumb (and one that we follow) is to keep your data in long format whenever you’re doing any data manipulation or processing and save wide format for displaying it.

Of course, there are exceptions and sometimes you have a reason not to do your processing in long format, for instance when a function requires a wide data frame.

That means that in reality, you’ll often find yourself wanting to switch between long and wide format. Fortunately, Fortunately, using the tidyr package, it is very simple to convert from long to wide format and back again.

Pivoting from long to wide

We’ve already seen an example of pivoting from long to wide format. Let’s explore that in a little more detail.

We use tidyr::pivot_wider to go from long to wide.

The most important arguments to the function are id_colsnames_from and values_from, and they each specify a column in our long dataframe.

  • The id_cols column specifies the unique identifier of each observation in our wide data frame.
  • The unique values in the names_from column become the column names in the wide data frame.
  • The values in the values_from column gets populated into the cells of the wide data frame.

In our example:

  • We want to index our wide dataframe by date, so we specify id_cols = date
  • We want the tickers to form columns in the wide dataframe, so we specify names_from = ticker
  • We want to populate our wide dataframe with returns values, so we specify values_from = returns

Here’s what that looks like:

dailyindex_df %>%
pivot_wider(id_cols = date, names_from = ticker, values_from = returns) %>%
kable() %>%
kable_styling(position = ‘center’) %>%
scroll_box(width = ‘800px’, height = ‘300px’)

date EQ_US EQ_NONUS_DEV EQ_EMER TN_US TB_US BOND_EMER GOLD
2020-03-02 0.0460048 0.0111621 0.0114468 -0.0008475 -0.0073579 0.0051502 0.0179376
2020-03-03 -0.0280740 0.0097820 0.0106093 0.0093299 0.0154987 0.0093937 0.0311450
2020-03-04 0.0422302 0.0062777 0.0097196 -0.0016807 -0.0099536 0.0059222 -0.0008743
2020-03-05 -0.0336854 -0.0014521 0.0014743 0.0050505 0.0227882 -0.0067283 0.0151949
2020-03-06 -0.0170369 -0.0232132 -0.0262282 0.0041876 0.0498034 -0.0076207 0.0026644
2020-03-09 -0.0761665 0.0000000 0.0000000 0.0041701 0.0262172 0.0000000 0.0018757
2020-03-10 0.0494037 0.0000000 0.0000000 -0.0091362 -0.0486618 -0.0477816 -0.0091271
2020-03-11 -0.0487713 0.0000000 0.0000000 -0.0025147 -0.0108696 -0.0206093 -0.0107857
2020-03-12 -0.0949084 0.0000000 0.0000000 0.0008403 -0.0155139 0.0000000 -0.0317549
2020-03-13 0.0931900 0.0000000 0.0000000 -0.0058774 -0.0216678 -0.0439158 -0.0462765
2020-03-16 -0.1197921 0.0000000 0.0000000 0.0126689 0.0510067 -0.0325359 -0.0203396
2020-03-17 0.0597801 0.0000000 0.0000000 -0.0133445 -0.0587484 -0.0197824 0.0265681
2020-03-18 -0.0517360 0.0000000 0.0000000 -0.0067625 -0.0434193 -0.0544904 -0.0311081
2020-03-19 0.0047010 0.0000000 0.0000000 0.0017021 0.0007092 -0.0234792 0.0011498
2020-03-20 -0.0431907 0.0000000 0.0000000 0.0144435 0.0652020 0.0142077 0.0039756
2020-03-23 -0.0292942 -0.2532532 0.0000000 0.0083752 0.0419162 -0.0215517 0.0568462
2020-03-24 0.0939740 0.0000000 0.0000000 -0.0041528 -0.0051086 0.0121145 0.0575354
2020-03-25 0.0115569 0.0000000 0.0000000 0.0008340 -0.0064185 0.0315560 -0.0174002
2020-03-26 0.0624207 0.0000000 0.0000000 0.0016667 0.0064599 0.0295359 0.0159455
2020-03-27 -0.0336616 0.0000000 0.0000000 0.0049917 0.0237484 -0.0081967 -0.0037858
2020-03-30 0.0336403 0.0000000 0.0000000 -0.0008278 -0.0050157 -0.0144628 -0.0065711
2020-03-31 -0.0159221 0.0000000 0.0000000 -0.0016570 -0.0144928 0.0115304 -0.0283711
2020-04-01 -0.0441380 0.0000000 0.0000000 0.0008299 0.0147059 -0.0124352 -0.0032808
2020-04-02 0.0230223 0.0000000 0.0000000 0.0008292 0.0056711 0.0000000 0.0291310

Could that be any easier?

Actually, yes!

id_cols defaults to any column or columns that aren’t specified by the names_from and values_from arguments. So in our case, we could actually not even bother with the id_cols argument:

date EQ_US EQ_NONUS_DEV EQ_EMER TN_US TB_US BOND_EMER GOLD
2020-03-02 0.0460048 0.0111621 0.0114468 -0.0008475 -0.0073579 0.0051502 0.0179376
2020-03-03 -0.0280740 0.0097820 0.0106093 0.0093299 0.0154987 0.0093937 0.0311450
2020-03-04 0.0422302 0.0062777 0.0097196 -0.0016807 -0.0099536 0.0059222 -0.0008743
2020-03-05 -0.0336854 -0.0014521 0.0014743 0.0050505 0.0227882 -0.0067283 0.0151949
2020-03-06 -0.0170369 -0.0232132 -0.0262282 0.0041876 0.0498034 -0.0076207 0.0026644
2020-03-09 -0.0761665 0.0000000 0.0000000 0.0041701 0.0262172 0.0000000 0.0018757
2020-03-10 0.0494037 0.0000000 0.0000000 -0.0091362 -0.0486618 -0.0477816 -0.0091271
2020-03-11 -0.0487713 0.0000000 0.0000000 -0.0025147 -0.0108696 -0.0206093 -0.0107857
2020-03-12 -0.0949084 0.0000000 0.0000000 0.0008403 -0.0155139 0.0000000 -0.0317549
2020-03-13 0.0931900 0.0000000 0.0000000 -0.0058774 -0.0216678 -0.0439158 -0.0462765
2020-03-16 -0.1197921 0.0000000 0.0000000 0.0126689 0.0510067 -0.0325359 -0.0203396
2020-03-17 0.0597801 0.0000000 0.0000000 -0.0133445 -0.0587484 -0.0197824 0.0265681
2020-03-18 -0.0517360 0.0000000 0.0000000 -0.0067625 -0.0434193 -0.0544904 -0.0311081
2020-03-19 0.0047010 0.0000000 0.0000000 0.0017021 0.0007092 -0.0234792 0.0011498
2020-03-20 -0.0431907 0.0000000 0.0000000 0.0144435 0.0652020 0.0142077 0.0039756
2020-03-23 -0.0292942 -0.2532532 0.0000000 0.0083752 0.0419162 -0.0215517 0.0568462
2020-03-24 0.0939740 0.0000000 0.0000000 -0.0041528 -0.0051086 0.0121145 0.0575354
2020-03-25 0.0115569 0.0000000 0.0000000 0.0008340 -0.0064185 0.0315560 -0.0174002
2020-03-26 0.0624207 0.0000000 0.0000000 0.0016667 0.0064599 0.0295359 0.0159455
2020-03-27 -0.0336616 0.0000000 0.0000000 0.0049917 0.0237484 -0.0081967 -0.0037858
2020-03-30 0.0336403 0.0000000 0.0000000 -0.0008278 -0.0050157 -0.0144628 -0.0065711
2020-03-31 -0.0159221 0.0000000 0.0000000 -0.0016570 -0.0144928 0.0115304 -0.0283711
2020-04-01 -0.0441380 0.0000000 0.0000000 0.0008299 0.0147059 -0.0124352 -0.0032808
2020-04-02 0.0230223 0.0000000 0.0000000 0.0008292 0.0056711 0.0000000 0.0291310

Same result as above. Brilliant.

Stay tuned for the next installment in which Kris will discuss Pivoting from wide to long.

Visit Robot Wealth website to read the full article and watch the instructional video: https://robotwealth.com/working-with-tidy-financial-data-in-tidyr/

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from Robot Wealth and is being posted with permission from Robot Wealth. The views expressed in this material are solely those of the author and/or Robot Wealth and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

trading top