O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Arithmetic and assignment

Check out the following example:

  > 2 + 2 
  [1] 4 
 
  > 9 / 3 
  [1] 3 
 
  > 5 %% 2    # modulus operator (remainder of 5 divided by 2) 
  [1] 1 

Anything that occurs after the octothorpe or pound sign, #, (or hash-tag for you young'uns), is ignored by the R interpreter. This is useful to document the code in natural language. These are called comments.

In a multi-operation arithmetic expression, R will follow the standard order of operations from math. In order to override this natural order, you have to use parentheses flanking the sub-expression that you'd like to be performed first:

   > 3 + 2 - 10 ^ 2        # ^ is the exponent operator 
   [1] -95 
   > 3 + (2 - 10) ^ 2 
   [1] 67 

In practice, almost all compound expressions are split up with intermediate ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required