Get startedGet started for free

Adding new columns

In a perfect world, you could be 100% certain that you will receive all of your cash flows. But, since these are predictions about the future, there is always a chance that someone won't be able to pay! You decide to run some analysis about a worst case scenario where you only receive half of your expected cash flow. To save the worst case scenario for later analysis, you decide to add it as a new column to the data frame!

cash$half_cash <- cash$cash_flow * .5

cash

  company cash_flow year half_cash
1       A      1000    1       500
2       A      4000    3      2000
3       A       550    4       275
4       B      1500    1       750
5       B      1100    2       550
6       B       750    4       375
7       B      6000    5      3000

And that's it! Creating new columns in your data frame is as simple as assigning the new information to data_frame$new_column. Often, the newly created column is some transformation of existing columns, so the $ operator really comes in handy here!

This exercise is part of the course

Introduction to R for Finance

View Course

Exercise instructions

  • Create a new worst case scenario where you only receive 25% of your expected cash flow, add it to the data frame as quarter_cash.
  • What if it took twice as long (in terms of year) to receive your money? Add a new column double_year with this scenario.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Quarter cash flow scenario
cash$quarter_cash <-

# Double year scenario
Edit and Run Code