Subsets in R
As you might imagine, selecting a specific column from a data frame is a common manipulation. So common, in fact, that it was given its own shortcut, the
$. The following return the same answer:
cash$cash_flow  1000 4000 550 1500 1100 750 6000 cash[,"cash_flow"]  1000 4000 550 1500 1100 750 6000
Useful right? Try it out!
Often, just simply selecting a column from a data frame is not all you want to do. What if you are only interested in the cash flows from company A? For more flexibility, try subset()!
subset(cash, company == "A") company cash_flow year 1 A 1000 1 2 A 4000 3 3 A 550 4
There are a few important things happening here:
- The first argument you pass to
subset()is the name of your data frame,
- Notice that you shouldn't put
==is the equality operator. It tests to find where two things are equal, and returns a logical vector.
TRY IT YOURSELF: Access the exercise in our Introduction to R for Finance course here.
subset()to select only the rows of
cashcorresponding to company B.
subset()rows that have cash flows due in 1 year.
# Rows about company B # Rows with cash flows due in 1 year
To learn more about accessing and subsetting dataframes in R, please see this video from our course Introduction to R for Finance.