Accessing and subsetting data frames (3)
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,cash. - Notice that you shouldn't put
companyin quotes! - The
==is the equality operator. It tests to find where two things are equal, and returns a logical vector. There is a lot more to learn about these relational operators, and you can learn all about them in the second finance course, Intermediate R for Finance!
This exercise is part of the course
Introduction to R for Finance
Exercise instructions
- Use
subset()to select only the rows ofcashcorresponding to company B. - Now
subset()rows that have cash flows due in 1 year.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Rows about company B
# Rows with cash flows due in 1 year