Accessing and subsetting data frames (2)
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!
This exercise is part of the course
Introduction to R for Finance
Exercise instructions
- Select the
"year"
column fromcash
using$
. - Select the
"cash_flow"
column fromcash
using$
and multiply it by 2. - You can delete a column by assigning it
NULL
. Run the code that deletes"company"
. - Now print out
cash
again.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Select the year column
# Select the cash_flow column and multiply by 2
# Delete the company column
cash$company <- NULL
# Print cash again