Exercise

Create your first data.frame()

Data frames are great because of their ability to hold a different type of data in each column. To get started, let's use the data.frame() function to create a data frame of your business's future cash flows. Here are the variables that will be in the data frame:

  • company - The company that is paying you the cash flow (A or B).
  • cash_flow - The amount of money a company will receive.
  • year - The number of years from now that you receive the cash flow.

To create the data frame, you do the following:

data.frame(company = c("A", "A", "B"), cash_flow = c(100, 200, 300), year = c(1, 3, 2))

  company cash_flow year
1       A       100    1
2       A       200    3
3       B       300    2

Like matrices, data frames are created from vectors, so this code would have also worked:

company <- c("A", "A", "B")
cash_flow <- c(100, 200, 300)
year <- c(1, 3, 2)

data.frame(company, cash_flow, year)

Instructions

100 XP
  • New company, cash_flow, and year variables have been defined for you.
  • Create another data frame containing company, cash_flow, and year in that order. Assign it to cash You will use this data frame throughout the rest of the chapter!
  • Print out cash to get a look at your shiny new data frame.