Primary exercises
- Create a new variable.
Make a variable heightInMeters
that contains your height expressed in meters.
Print the variable.
Find the variable and its value in the Environment pane.
heightInMeters <- 1.75
heightInMeters
[1] 1.75
- Use a variable in an expression.
Use the variable heightInMeters
to calculate your height in cm.
Calculate the height in cm once again and store the result in a variable heightInCentimeters
.
Show/print heightInCentimeters
.
heightInMeters * 100
[1] 175
heightInCentimeters <- heightInMeters * 100
heightInCentimeters
[1] 175
- Change a value of a variable.
Increase the height stored in heightInMeters
by 0.1m.
Check whether the value of the variable indeed changed.
Has the value of heightInCentimeters
also changed? Why?
heightInMeters <- heightInMeters + 0.1
heightInMeters
[1] 1.85
heightInCentimeters # not changed, for change assignment is needed
[1] 175
- Typo in variable name.
Observe the error shown when you try to print a variable but you make a typo in the variable name.
height.in.meters # Error: object 'height.in.meters' not found
Error in eval(expr, envir, enclos): object 'height.in.meters' not found
Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC