Primary exercises

  1. Simple expression.
    Calculate 25 plus the square of 12.
25 + 12^2
[1] 169
  1. Calling a function.
    What is the square root of the number you have just calculated (here type the number directly)?
sqrt( 25 + 12^2 )
[1] 13
  1. Prompt with +.
    In the console type an incomplete expression (1+2 and press Enter. Practice how to return to the proper console prompt >. Finally, clear the console.
# To stop in the console press: Esc or Ctrl-C
# To clear in the console press: Ctrl-L
  1. Some error messages.
    In the console type an error expression (1+2)*(3+4)). Understand the error message. Also understand errors reported for expressions: 0,25, sin(), xyz.
# (1+2)*(3+4))     -> forgotten (
# 0,25             -> use dot as decimal point: 0.25
# sin()            -> sin needs an argument
# xyz              -> xyz is a name not known to R
  1. Getting help.
    Find the manual page for the sin function.
# In the console type: ?sin

Extra exercises

  1. Command history.
    In the console try: history(). Locate the History pane and observe what happens when you double-click a line there.
    A save button allows to save typed commands but this is not a practical way to save the code.
# history() in RStudio simply shows History pane
# double click copies a line from History to the Console
# we will learn to write scripts instead of saving History
  1. Logarithms and exponentials.
    Check the manual to understand the difference between log10( 100 ), log2( 100 ) and log( 100 ).
    Find what would be the result of exp( 1 ) and log( exp( 1 ) ).
log10( 100 )    # logarithm base 10 of 100, 10^2 = 100
[1] 2
log2( 100 )     # logarithm base 2 of 100
[1] 6.643856
log( 100 )      # natural logarithm of 100
[1] 4.60517
exp( 1 )        # base of natural logarithm, e^1
[1] 2.718282
log( exp( 1 ) ) # log is inverse function of exp
[1] 1
  1. Trigonometry.
    Calculate \(cos(90^{\circ})\).
    Remember that in R the trigonometric functions require angles specified in radians (you may use the pi variable).
cos( 90 / 180 * pi ) # conversion from degrees to radians
[1] 6.123234e-17
                     # the result is very close to zero, but not exact zero
                     # the numeric calculations are always approximate


Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC