Exercises for 1.1: Getting started

Exercise 1.1.1: Addition

The first command is simple addition, so the exercise is to add some numbers together. Open up R, and use it to do a few basic sums. In particular, it’s worth trying the following:

  • Try adding more than two numbers.
  • Try playing around with the number of spaces between numbers and the plus sign, and verify that it doesn’t actually matter.
  • Try hitting enter when you’re half way through the command (e.g., 10+). R will wait for you to finish before trying to execute it.

Exercise 1.1.2: Other arithmetic operations

  • Multiply 3 by 14 and subtract 5 from the result
  • Raise 5 to the power of 4
  • Divide 267563 by 1235

Exercise 1.1.3: Logical operations

  • Use logical operations to get R to agree that “two plus two equals five” is FALSE
  • Use logical operations to test whether 8 raised to the power 13 is less than 15 ^ 9

Exercise 1.1.4: Using functions

  • use the sqrt()function to calculate the square root of 789
  • round 2.456 to the nearest whole number using round()
  • round 2.456 to two decimal places using round()
  • R has a factorial()function that calculates the factorial function, n!(e.g., 5! = 5*4*3*2*1). Use it to calculate 25!, and take note of the way that R formats the answer.
  • Use factorial()to (try to) calculate 2000!, and note the answer that R gives.