Basic arithmetic and logic

R as a pocket calculator

Arithmetic operators

See help(Arithmetic)

 10
 3
 159
 3
 1

Also see help(Syntax) for operator precendence / associativity: This determines the order in which computations are carried out when multiple operators are present. Use parentheses to make execution order explicit.

 45
 29

Using operators in function notation

 0.1
 5

 2
 1
 4
 2
 2.718282

 1
 3.142
 2
 3
 22

 -1+0i
 0+0i
 NaN
 0+1i

 2147483647
 2.220446e-16
 Inf
 TRUE
 NaN
 TRUE
NULL
 TRUE

 4
 93

Logic

 TRUE
 FALSE
 FALSE
 TRUE
 TRUE
 FALSE

Logical comparisons

 TRUE
 FALSE
 FALSE
 TRUE
 TRUE
 FALSE
 FALSE
 FALSE
 TRUE
 TRUE
 FALSE
 TRUE
 TRUE
 FALSE

Short-circuit logical comparisons with vectors

 TRUE
 FALSE

 TRUE
 FALSE
 FALSE
 TRUE

Checking whether any or all elements are TRUE

 FALSE
 TRUE
 FALSE
 TRUE

In an empty vector, there is no element that is FALSE, therefore:

 TRUE

In an empty vector, you cannot pick an element that is TRUE, therefore:

 FALSE

Numeric representations

 TRUE
 FALSE

Floating point arithmetic

 FALSE
 9
 1.224647e-16
 1.110223e-16
 0

What every computer scientist should know about floating-point arithmetic

Checking decimal numbers for equality

 TRUE
 FALSE
 "Mean relative difference: 8.100445e-08"
 FALSE