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.
tidyr::pivot_wider to go from long to wide.
The most important arguments to the function are
values_from, and they each specify a column in our long dataframe.
id_colscolumn specifies the unique identifier of each observation in our wide data frame.
- The unique values in the
names_fromcolumn become the column names in the wide data frame.
- The values in the
values_fromcolumn 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:
pivot_wider(id_cols = date, names_from = ticker, values_from = returns) %>%
kable_styling(position = ‘center’) %>%
scroll_box(width = ‘800px’, height = ‘300px’)
Could that be any easier?
id_cols defaults to any column or columns that aren’t specified by the
values_from arguments. So in our case, we could actually not even bother with the
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.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.