Tutorials
r programming

Subsets in R

Learn to access and subset dataframes 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

[1] 1000 4000  550 1500 1100  750 6000

cash[,"cash_flow"]

[1] 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, cash.
  • Notice that you shouldn't put company in quotes!
  • The == is the equality operator. It tests to find where two things are equal, and returns a logical vector.

Exercise

TRY IT YOURSELF: Access the exercise in our Introduction to R for Finance course here.

  • Use subset() to select only the rows of cash corresponding to company B.
  • Now subset() rows that have cash flows due in 1 year.
# Rows about company B


# Rows with cash flows due in 1 year

Video

To learn more about accessing and subsetting dataframes in R, please see this video from our course Introduction to R for Finance.

This content is taken from DataCamp’s Introduction to R for Finance course by Lore Dirick.