A few list creating functions
1. A few list creating functions
Congrats! You only have a few exercises to complete before finishing the course. We have two interesting functions left to cover, so hang on a little longer! There are a number of functions that return lists. One of these is split. You can use split to break up a data frame into multiple smaller data frames, defined by some grouping.2. split() it up
Let's bring back your friends Dan and Rob one last time to demonstrate this. Take a look at the debt data frame again. Let's try and use split to break up the debt data frame into two smaller data frames, grouped by who owes you money.3. split() it up
First, we create a grouping, the names. Then we pass debt and the grouping as arguments to the split function and watch it work its magic! As you can see, it has returned a list of two smaller data frames, split by name.4. split() it up
split even names each element of the list by the grouping that it split on! This means that you can immediately access the Dan data frame by doing split_debt dollar sign Dan. To dig even deeper and access the data inside the Dan data frame, you can use two dollar signs. The first gains you access to the Dan data frame, and the second selects the payment column from that data frame. To put the split list back together into one data frame, use the unsplit function in the same way that you used split.5. split() example
Great, so how could this be useful? Well, imagine if you had to perform a calculation that was unique based on who owed you money. For example, what if you decided to give Dan a discount of 20%, and Rob a discount of 10% and save that information in a new column called new_payment? One way to accomplish this is to split the data frame by name, apply the discounts to each data frame separately, and combine the data frames back in the end. This type of thinking is actually so common, that it has become a class of problems known as split-apply-combine. In _Intermediate R for Finance_, you will learn an entire family of efficient functions dedicated to these problems, but for now, let's see a rough example of how one could use this idea.6. split-apply-combine
First define a grouping and split the data frame like before. Then, create a new column in each data frame using double dollar signs to apply the unique discount. Looking at the list now, we can see the newly created column.7. split-apply-combine
Finally, you can re combine everything back together using the unsplit function.8. Attributes
To finish up, let's quickly talk about attributes. Attributes are extra bits of data about your data structure that help define some of its basic features. For example, matrices have a dim attribute defining the row and column dimensions of the matrix. Data frames have names and row-dot-names attributes which list their column and row names respectively. You will explore attributes a bit further in the exercises.9. Let's practice!
You're almost done with Introduction to R for Finance! Good luck with the last few exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.