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)
This exercise is part of the course
Introduction to R for Finance
Exercise instructions
- New
company
,cash_flow
, andyear
variables have been defined for you. - Create another data frame containing
company
,cash_flow
, andyear
in that order. Assign it tocash
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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Variables
company <- c("A", "A", "A", "B", "B", "B", "B")
cash_flow <- c(1000, 4000, 550, 1500, 1100, 750, 6000)
year <- c(1, 3, 4, 1, 2, 4, 5)
# Data frame
cash <-
# Print cash