Assignment and variables (1)
It looks like you're becoming an expert at using R as a calculator! Time to take it one step further. These numbers you are calculating haven't been very descriptive. 5? 5 what? 5 apples? 5 monkeys? What if you could assign that 5 a descriptive name like number_of_apples
, and then simply type that name whenever you want to use 5? Enter, variables.
A variable allows you to store a value or an object in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable. You use <-
to assign a variable:
my_money <- 100
This exercise is part of the course
Introduction to R for Finance
Exercise instructions
- Assign a value of 200 to the
savings
variable in the script. - Press Submit Answer and note how simply typing
savings
in the script asks R prints the value to the console!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Assign 200 to savings
savings <-
# Print the value of savings to the console
savings