LoslegenKostenlos loslegen

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 company in 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!

Diese Übung ist Teil des Kurses

Introduction to R for Finance

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# Rows about company B


# Rows with cash flows due in 1 year
Code bearbeiten und ausführen