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!
Cet exercice fait partie du cours
Introduction to R for Finance
Instructions
- Select the
"year"column fromcashusing$. - Select the
"cash_flow"column fromcashusing$and multiply it by 2. - You can delete a column by assigning it
NULL. Run the code that deletes"company". - Now print out
cashagain.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Select the year column
# Select the cash_flow column and multiply by 2
# Delete the company column
cash$company <- NULL
# Print cash again